Value Object Collections

Collections are array like classes that are used to hold multiple instances of a single type of VO. Unlike an array though, the collections know what kind of class they're designed for and won't allow the wrong type to be added to them. The collections can also be iterated over in a foreach loop, like arrays, and provide other convenience methods.

It should also be noted that the collection objects use lazy instantiation. Each collection returned by a query contains the raw data and object data. When a query for a table is retrieved using its selector object, the raw result data is given to the collection in its constructor. The collection does not construct any VO instance until it is specifically requested. This prevents resources (CPU cycles and memory) from being used unless they are actually needed.

This may not benefit much in a lot of scripts, but if you fetch 10000 results and are only looking for the first match (at index #i), then only i rows are converted to objects and the resources needed to instantiate the other 10000 - i objects remain free to do other work.

Getting a collection via the Table's Selector class

Our first collection will be the result of selecting a single `user_id` from the `user` table via a “UserSelector” instance. Selectors always return a collection, so that there's no doubt about the return type of the fetch() method (single VO or multiple).

Note that the last line of output “Built UserVO Objects (Total = 0 of 1):” is saying that the collection has data for 1 UserVO object and has instantiated 0.

$userPicker = new UserSelector(2);
$user2Collection = $userPicker->fetch();
echo $user2Collection;
$freshCollection = new UserCollection(); AbstractSelector::fetch() using query: “SELECT * FROM `user` WHERE `user`.`user_id` = 2”
AbstractCollection::__toString()
Raw VO Data (Total = 1):
Raw 0: Array ( [user_id] => 2 [username] => Method3User [pwd] => Method3Pwd )
Built UserVO Objects (Total = 0 of 1):

A larger collection via the table selector

Now we will make a query on the table that will return multiple rows.

$resultCollection = $userPicker->select()->customColumn("CHAR_LENGTH(`pwd`) AS `pwdLength`")->userId(">", 2)->limit(0,15)->orderBy("`pwdLength`", "ASC")->fetch();
echo $resultCollection; AbstractSelector::fetch() using query: “SELECT *, CHAR_LENGTH(`pwd`) AS `pwdLength` FROM `user` WHERE `user`.`user_id` > 2 ORDER BY `pwdLength` ASC LIMIT 0, 15 ”
AbstractCollection::__toString()
Raw VO Data (Total = 12):
Raw 0: Array ( [user_id] => 6 [username] => Bob_260 [pwd] => 16_6 [pwdLength] => 4 )
Raw 1: Array ( [user_id] => 4 [username] => MyName_234 [pwd] => a87_4 [pwdLength] => 5 )
Raw 2: Array ( [user_id] => 5 [username] => YourName_135 [pwd] => e4d_5 [pwdLength] => 5 )
Raw 3: Array ( [user_id] => 3 [username] => Therese_25 [pwd] => eccb_3 [pwdLength] => 6 )
Raw 4: Array ( [user_id] => 7 [username] => YourName_135 [pwd] => 8f14_7 [pwdLength] => 6 )
Raw 5: Array ( [user_id] => 41918 [username] => theUsername [pwd] => thePwd [pwdLength] => 6 )
Raw 6: Array ( [user_id] => 8 [username] => Anaximenes_113 [pwd] => c9f0f_8 [pwdLength] => 7 )
Raw 7: Array ( [user_id] => 41928 [username] => Username8 [pwd] => c9f0f89 [pwdLength] => 7 )
Raw 8: Array ( [user_id] => 41926 [username] => Username6 [pwd] => 1679091 [pwdLength] => 7 )
Raw 9: Array ( [user_id] => 41924 [username] => Username4 [pwd] => a87ff67 [pwdLength] => 7 )
Raw 10: Array ( [user_id] => 41923 [username] => Username3 [pwd] => eccbc87 [pwdLength] => 7 )
Raw 11: Array ( [user_id] => 41922 [username] => Username2 [pwd] => c81e728 [pwdLength] => 7 )
Built UserVO Objects (Total = 0 of 12):

Getting objects from a collection and adding objects to a collection.

Now we will put a few of the results from this collection into a new empty collection.

$freshCollection = new UserCollection();//Create an empty collection
$random = $resultCollection->random();//Copy a random VO from our previous result collection
$number3 = $resultCollection->pop(3);//Remove the third VO from the previous collection
$last = $resultCollection->pop();//Remove the last VO from the collection
//Add all of them to the new collection
$freshCollection->push($random);
$freshCollection->push($number3);
$freshCollection->push($last); AbstractCollection::getRow()
Building and returning UserVO #2
AbstractDbTableMap::getVO() delegating to concrete class....
UserDbMap::getInstance() Creating new UserVO and calling init()
UserVO::init() Setting Properties With: Array ( [user_id] => 5 [username] => YourName_135 [pwd] => e4d_5 [pwdLength] => 5 )
AbstractCollection::pop() getting index: 3 of {0 - 11}
AbstractCollection::getRow()
Building and returning UserVO #3
AbstractDbTableMap::getVO() delegating to concrete class....
UserDbMap::getInstance() Creating new UserVO and calling init()
UserVO::init() Setting Properties With: Array ( [user_id] => 3 [username] => Therese_25 [pwd] => eccb_3 [pwdLength] => 6 )
AbstractCollection::pop() Unsetting (UserVO) { Key: 3, user_id: 3, username: “Therese_25”, pwd: “eccb_3” }
Removing the object at index #3 from the collection.
New Collection Total: 11
AbstractCollection::pop() getting index: 10 of {0 - 10}
AbstractCollection::getRow()
Building and returning UserVO #10
AbstractDbTableMap::getVO() delegating to concrete class....
UserDbMap::getInstance() Creating new UserVO and calling init()
UserVO::init() Setting Properties With: Array ( [user_id] => 41922 [username] => Username2 [pwd] => c81e728 [pwdLength] => 7 )
AbstractCollection::pop() Unsetting (UserVO) { Key: 41922, user_id: 41922, username: “Username2”, pwd: “c81e728” }
Removing the object at index #10 from the collection.
New Collection Total: 10
AbstractCollection::push() adding UserVO.5 to the collection
AbstractValueObject::toArray() converting (UserVO) { Key: 5, user_id: 5, username: “YourName_135”, pwd: “e4d_5” } to an associative array...
UserDbMap::voToArray() converting UserVO.5 back to an associative array
AbstractCollection::push() adding UserVO.3 to the collection
AbstractValueObject::toArray() converting (UserVO) { Key: 3, user_id: 3, username: “Therese_25”, pwd: “eccb_3” } to an associative array...
UserDbMap::voToArray() converting UserVO.3 back to an associative array
AbstractCollection::push() adding UserVO.41922 to the collection
AbstractValueObject::toArray() converting (UserVO) { Key: 41922, user_id: 41922, username: “Username2”, pwd: “c81e728” } to an associative array...
UserDbMap::voToArray() converting UserVO.41922 back to an associative array

The random() method of the Collection classes also optionally accepts an integer parameter n. If given, the random method will return a new Collection instance of the same type with n random VO instances already in it.

Now we can echo both collections to get a look at what is in them. We removed two VOs from the previous collection, so the total objects should be two less than above. (We only copied the random VO)

echo "<hr/>Previous Result Collection:<br/>$resultCollection<hr/>";
echo "<hr/>New Collection:<br/>$freshCollection<hr/>";

Previous Result Collection:

AbstractCollection::__toString()
Raw VO Data (Total = 10):
Raw 0: Array ( [user_id] => 6 [username] => Bob_260 [pwd] => 16_6 [pwdLength] => 4 )
Raw 1: Array ( [user_id] => 4 [username] => MyName_234 [pwd] => a87_4 [pwdLength] => 5 )
Raw 2: Array ( [user_id] => 5 [username] => YourName_135 [pwd] => e4d_5 [pwdLength] => 5 )
Raw 3: Array ( [user_id] => 7 [username] => YourName_135 [pwd] => 8f14_7 [pwdLength] => 6 )
Raw 4: Array ( [user_id] => 41918 [username] => theUsername [pwd] => thePwd [pwdLength] => 6 )
Raw 5: Array ( [user_id] => 8 [username] => Anaximenes_113 [pwd] => c9f0f_8 [pwdLength] => 7 )
Raw 6: Array ( [user_id] => 41928 [username] => Username8 [pwd] => c9f0f89 [pwdLength] => 7 )
Raw 7: Array ( [user_id] => 41926 [username] => Username6 [pwd] => 1679091 [pwdLength] => 7 )
Raw 8: Array ( [user_id] => 41924 [username] => Username4 [pwd] => a87ff67 [pwdLength] => 7 )
Raw 9: Array ( [user_id] => 41923 [username] => Username3 [pwd] => eccbc87 [pwdLength] => 7 )
Built UserVO Objects (Total = 1 of 10):
VO 0: (UserVO) { Key: 5, user_id: 5, username: “YourName_135”, pwd: “e4d_5” }


New Collection:

AbstractCollection::__toString()
Raw VO Data (Total = 3):
Raw 0: Array ( [user_id] => 5 [username] => YourName_135 [pwd] => e4d_5 )
Raw 1: Array ( [user_id] => 3 [username] => Therese_25 [pwd] => eccb_3 )
Raw 2: Array ( [user_id] => 41922 [username] => Username2 [pwd] => c81e728 )
Built UserVO Objects (Total = 3 of 3):
VO 0: (UserVO) { Key: 5, user_id: 5, username: “YourName_135”, pwd: “e4d_5” }
VO 1: (UserVO) { Key: 3, user_id: 3, username: “Therese_25”, pwd: “eccb_3” }
VO 2: (UserVO) { Key: 41922, user_id: 41922, username: “Username2”, pwd: “c81e728” }

Using the collection in a foreach loop

We'll loop through the $freshCollection of users set their usernames, update them in the database, and then remove them from the collection.

Note that the raw array values don't match the object array values afterward, but this is alright, because the collection will only ever use the built objects, unless it is instantiating new VOs.

foreach ($freshCollection as $vo) {
    $nameAry = explode("_", $vo->getUsername());
    $newName = $nameAry[0] . "_" . rand(0,321);
    echo "<hr/><b>Updating</b> username: “$vo->getUsername()” to “$newName”<br/>";
    /* @var $vo UserVO */
    $vo->setUsername($newName)->updateNow();
}
echo $freshCollection; AbstractCollection::valid() validating #0 (Result: 1)
AbstractCollection::getRow()
Returning already built UserVO #0

Updating username: “YourName_135” to “YourName_135”
UserVO::setUsername() setting new value: “YourName_135”
AbstractValueObject::updateNow() delegating to concrete map...
UserDbMap::updateNow() Updated: (UserVO) { Key: 5, user_id: 5, username: “YourName_135”, pwd: “e4d_5” }
AbstractCollection::valid() validating #1 (Result: 1)
AbstractCollection::getRow()
Returning already built UserVO #1

Updating username: “Therese_25” to “Therese_161”
UserVO::setUsername() setting new value: “Therese_161”
AbstractValueObject::updateNow() delegating to concrete map...
UserDbMap::updateNow() Updated: (UserVO) { Key: 3, user_id: 3, username: “Therese_161”, pwd: “eccb_3” }
AbstractCollection::valid() validating #2 (Result: 1)
AbstractCollection::getRow()
Returning already built UserVO #2

Updating username: “Username2” to “Username2_120”
UserVO::setUsername() setting new value: “Username2_120”
AbstractValueObject::updateNow() delegating to concrete map...
UserDbMap::updateNow() Updated: (UserVO) { Key: 41922, user_id: 41922, username: “Username2_120”, pwd: “c81e728” }
AbstractCollection::valid() validating #3 (Result: )
AbstractCollection::__toString()
Raw VO Data (Total = 3):
Raw 0: Array ( [user_id] => 5 [username] => YourName_135 [pwd] => e4d_5 )
Raw 1: Array ( [user_id] => 3 [username] => Therese_25 [pwd] => eccb_3 )
Raw 2: Array ( [user_id] => 41922 [username] => Username2 [pwd] => c81e728 )
Built UserVO Objects (Total = 3 of 3):
VO 0: (UserVO) { Key: 5, user_id: 5, username: “YourName_135”, pwd: “e4d_5” }
VO 1: (UserVO) { Key: 3, user_id: 3, username: “Therese_161”, pwd: “eccb_3” }
VO 2: (UserVO) { Key: 41922, user_id: 41922, username: “Username2_120”, pwd: “c81e728” }

We can also reverse the direction that the collection iterates in.

foreach ($freshCollection as $key => $vo) {
/* @var $vo UserVO */
echo "User " . $vo->getUserId() . "<br/>";
} AbstractCollection::valid() validating #0 (Result: 1)
AbstractCollection::getRow()
Returning already built UserVO #0
User 5
AbstractCollection::valid() validating #1 (Result: 1)
AbstractCollection::getRow()
Returning already built UserVO #1
User 3
AbstractCollection::valid() validating #2 (Result: 1)
AbstractCollection::getRow()
Returning already built UserVO #2
User 41922
AbstractCollection::valid() validating #3 (Result: )
And now we switch directions
Note: The collection will remain reversed until forward() has been called on it.
$freshCollection->reverse();
foreach ($freshCollection as $key => $vo) {
/* @var $vo UserVO */
echo "User " . $vo->getUserId() . "<br/>";
} AbstractCollection::valid() validating #2 (Result: 1)
AbstractCollection::getRow()
Returning already built UserVO #2
User 41922
AbstractCollection::valid() validating #1 (Result: 1)
AbstractCollection::getRow()
Returning already built UserVO #1
User 3
AbstractCollection::valid() validating #0 (Result: 1)
AbstractCollection::getRow()
Returning already built UserVO #0
User 5
AbstractCollection::valid() validating #-1 (Result: )

Getting Collections from Collections

As mentioned before, if random() is supplied a positive integer n, it will return a new collection with n random VOs pre-loaded from the original collection.

Collections also have a filterAryWith($funcName) method that accepts a function name and uses it to build a new collection. filterWith() will test each VO that it contains and if $funcName($vo) returns true, then that $vo will be included in the filtered collection that is returned.

function userHasWeakPwd(UserVO $vo){
    return strlen($vo->getPwd()) < 6;//For example we'll just filter on pwd length.
}
$filteredCollection = $resultCollection->filterWith("userHasWeakPwd");
echo "<h2>Filtered VO Collection (pwd < 6 characters):</h2> $filteredCollection"; AbstractCollection::getRow()
Returning already built UserVO #0
AbstractCollection::filterVOsWith()
...checking UserVO.5 with userHasWeakPwd()
AbstractCollection::push() adding UserVO.5 to the collection
AbstractValueObject::toArray() converting (UserVO) { Key: 5, user_id: 5, username: “YourName_135”, pwd: “e4d_5” } to an associative array...
UserDbMap::voToArray() converting UserVO.5 back to an associative array
AbstractCollection::getRow()
Building and returning UserVO #1
AbstractDbTableMap::getVO() delegating to concrete class....
UserDbMap::getInstance() Creating new UserVO and calling init()
UserVO::init() Setting Properties With: Array ( [user_id] => 4 [username] => MyName_234 [pwd] => a87_4 [pwdLength] => 5 )
AbstractCollection::filterVOsWith()
...checking UserVO.4 with userHasWeakPwd()
AbstractCollection::push() adding UserVO.4 to the collection
AbstractValueObject::toArray() converting (UserVO) { Key: 4, user_id: 4, username: “MyName_234”, pwd: “a87_4” } to an associative array...
UserDbMap::voToArray() converting UserVO.4 back to an associative array
AbstractCollection::getRow()
Building and returning UserVO #2
AbstractDbTableMap::getVO() delegating to concrete class....
UserDbMap::getInstance() Creating new UserVO and calling init()
UserVO::init() Setting Properties With: Array ( [user_id] => 5 [username] => YourName_135 [pwd] => e4d_5 [pwdLength] => 5 )
AbstractCollection::filterVOsWith()
...checking UserVO.5 with userHasWeakPwd()
AbstractCollection::push() adding UserVO.5 to the collection
AbstractValueObject::toArray() converting (UserVO) { Key: 5, user_id: 5, username: “YourName_135”, pwd: “e4d_5” } to an associative array...
UserDbMap::voToArray() converting UserVO.5 back to an associative array
AbstractCollection::getRow()
Building and returning UserVO #3
AbstractDbTableMap::getVO() delegating to concrete class....
UserDbMap::getInstance() Creating new UserVO and calling init()
UserVO::init() Setting Properties With: Array ( [user_id] => 7 [username] => YourName_135 [pwd] => 8f14_7 [pwdLength] => 6 )
AbstractCollection::filterVOsWith()
...checking UserVO.7 with userHasWeakPwd()
AbstractCollection::getRow()
Building and returning UserVO #4
AbstractDbTableMap::getVO() delegating to concrete class....
UserDbMap::getInstance() Creating new UserVO and calling init()
UserVO::init() Setting Properties With: Array ( [user_id] => 41918 [username] => theUsername [pwd] => thePwd [pwdLength] => 6 )
AbstractCollection::filterVOsWith()
...checking UserVO.41918 with userHasWeakPwd()
AbstractCollection::getRow()
Building and returning UserVO #5
AbstractDbTableMap::getVO() delegating to concrete class....
UserDbMap::getInstance() Creating new UserVO and calling init()
UserVO::init() Setting Properties With: Array ( [user_id] => 8 [username] => Anaximenes_113 [pwd] => c9f0f_8 [pwdLength] => 7 )
AbstractCollection::filterVOsWith()
...checking UserVO.8 with userHasWeakPwd()
AbstractCollection::getRow()
Building and returning UserVO #6
AbstractDbTableMap::getVO() delegating to concrete class....
UserDbMap::getInstance() Creating new UserVO and calling init()
UserVO::init() Setting Properties With: Array ( [user_id] => 41928 [username] => Username8 [pwd] => c9f0f89 [pwdLength] => 7 )
AbstractCollection::filterVOsWith()
...checking UserVO.41928 with userHasWeakPwd()
AbstractCollection::getRow()
Building and returning UserVO #7
AbstractDbTableMap::getVO() delegating to concrete class....
UserDbMap::getInstance() Creating new UserVO and calling init()
UserVO::init() Setting Properties With: Array ( [user_id] => 41926 [username] => Username6 [pwd] => 1679091 [pwdLength] => 7 )
AbstractCollection::filterVOsWith()
...checking UserVO.41926 with userHasWeakPwd()
AbstractCollection::getRow()
Building and returning UserVO #8
AbstractDbTableMap::getVO() delegating to concrete class....
UserDbMap::getInstance() Creating new UserVO and calling init()
UserVO::init() Setting Properties With: Array ( [user_id] => 41924 [username] => Username4 [pwd] => a87ff67 [pwdLength] => 7 )
AbstractCollection::filterVOsWith()
...checking UserVO.41924 with userHasWeakPwd()
AbstractCollection::getRow()
Building and returning UserVO #9
AbstractDbTableMap::getVO() delegating to concrete class....
UserDbMap::getInstance() Creating new UserVO and calling init()
UserVO::init() Setting Properties With: Array ( [user_id] => 41923 [username] => Username3 [pwd] => eccbc87 [pwdLength] => 7 )
AbstractCollection::filterVOsWith()
...checking UserVO.41923 with userHasWeakPwd()

Filtered VO Collection (pwd < 6 characters):

AbstractCollection::__toString()
Raw VO Data (Total = 3):
Raw 0: Array ( [user_id] => 5 [username] => YourName_135 [pwd] => e4d_5 )
Raw 1: Array ( [user_id] => 4 [username] => MyName_234 [pwd] => a87_4 )
Raw 2: Array ( [user_id] => 5 [username] => YourName_135 [pwd] => e4d_5 )
Built UserVO Objects (Total = 3 of 3):
VO 0: (UserVO) { Key: 5, user_id: 5, username: “YourName_135”, pwd: “e4d_5” }
VO 1: (UserVO) { Key: 4, user_id: 4, username: “MyName_234”, pwd: “a87_4” }
VO 2: (UserVO) { Key: 5, user_id: 5, username: “YourName_135”, pwd: “e4d_5” }

Collections also have a filterRawWith($funcName) method, which is identical to filterWith(), except that filterRawWith() passes the raw arrays to $funcName and does not instantiate the objects to be included. This will save overhead if the actual VOs never need to be constructed. If they are ever needed, the returned collection will instantiate the objects just as the original would have.

function userHasStrongPwd(array $rawAry){
    return strlen($rawAry['pwd']) >= 6;//Here we perform the “opposite” test on the pwd.
}
$filteredCollection2 = $resultCollection->filterRawWith("userHasStrongPwd");
echo "<h2>Filtered “Raw” Collection (pwd >= 6 characters):</h2> $filteredCollection2"; AbstractCollection::filterRawWith()
...checking: Array ( [user_id] => 6 [username] => Bob_260 [pwd] => 16_6 [pwdLength] => 4 ) with userHasStrongPwd()
AbstractCollection::filterRawWith()
...checking: Array ( [user_id] => 4 [username] => MyName_234 [pwd] => a87_4 [pwdLength] => 5 ) with userHasStrongPwd()
AbstractCollection::filterRawWith()
...checking: Array ( [user_id] => 5 [username] => YourName_135 [pwd] => e4d_5 [pwdLength] => 5 ) with userHasStrongPwd()
AbstractCollection::filterRawWith()
...checking: Array ( [user_id] => 7 [username] => YourName_135 [pwd] => 8f14_7 [pwdLength] => 6 ) with userHasStrongPwd()
AbstractCollection::filterRawWith()
...checking: Array ( [user_id] => 41918 [username] => theUsername [pwd] => thePwd [pwdLength] => 6 ) with userHasStrongPwd()
AbstractCollection::filterRawWith()
...checking: Array ( [user_id] => 8 [username] => Anaximenes_113 [pwd] => c9f0f_8 [pwdLength] => 7 ) with userHasStrongPwd()
AbstractCollection::filterRawWith()
...checking: Array ( [user_id] => 41928 [username] => Username8 [pwd] => c9f0f89 [pwdLength] => 7 ) with userHasStrongPwd()
AbstractCollection::filterRawWith()
...checking: Array ( [user_id] => 41926 [username] => Username6 [pwd] => 1679091 [pwdLength] => 7 ) with userHasStrongPwd()
AbstractCollection::filterRawWith()
...checking: Array ( [user_id] => 41924 [username] => Username4 [pwd] => a87ff67 [pwdLength] => 7 ) with userHasStrongPwd()
AbstractCollection::filterRawWith()
...checking: Array ( [user_id] => 41923 [username] => Username3 [pwd] => eccbc87 [pwdLength] => 7 ) with userHasStrongPwd()

Filtered “Raw” Collection (pwd >= 6 characters):

AbstractCollection::__toString()
Raw VO Data (Total = 7):
Raw 0: Array ( [user_id] => 7 [username] => YourName_135 [pwd] => 8f14_7 [pwdLength] => 6 )
Raw 1: Array ( [user_id] => 41918 [username] => theUsername [pwd] => thePwd [pwdLength] => 6 )
Raw 2: Array ( [user_id] => 8 [username] => Anaximenes_113 [pwd] => c9f0f_8 [pwdLength] => 7 )
Raw 3: Array ( [user_id] => 41928 [username] => Username8 [pwd] => c9f0f89 [pwdLength] => 7 )
Raw 4: Array ( [user_id] => 41926 [username] => Username6 [pwd] => 1679091 [pwdLength] => 7 )
Raw 5: Array ( [user_id] => 41924 [username] => Username4 [pwd] => a87ff67 [pwdLength] => 7 )
Raw 6: Array ( [user_id] => 41923 [username] => Username3 [pwd] => eccbc87 [pwdLength] => 7 )
Built UserVO Objects (Total = 0 of 7):

Using Collections in a for loop and emptying them

Now we will empty the collection one at a time.
Note: This does not effect the database. It simply pulls instances out of the collection.

$ttl = $freshCollection->count();
echo "Count: " . $ttl . "<hr/>";
for($idx = 0; $idx < $ttl; $idx++){
    $first = $freshCollection->shift();//Remove & return the first item in the collection.
    echo "<hr/>Removed: $first<hr/>";
}
echo "<h3>Final Result Collection:</h3> $freshCollection"; Count: 3
AbstractCollection::getRow()
Returning already built UserVO #0
Removing the object at index #0 from the collection.
New Collection Total: 2
AbstractCollection::shift()
Returning: (UserVO) { Key: 5, user_id: 5, username: “YourName_135”, pwd: “e4d_5” }

Removed: (UserVO) { Key: 5, user_id: 5, username: “YourName_135”, pwd: “e4d_5” }
AbstractCollection::getRow()
Returning already built UserVO #0
Removing the object at index #0 from the collection.
New Collection Total: 1
AbstractCollection::shift()
Returning: (UserVO) { Key: 3, user_id: 3, username: “Therese_161”, pwd: “eccb_3” }

Removed: (UserVO) { Key: 3, user_id: 3, username: “Therese_161”, pwd: “eccb_3” }
AbstractCollection::getRow()
Returning already built UserVO #0
Removing the object at index #0 from the collection.
New Collection Total: 0
AbstractCollection::shift()
Returning: (UserVO) { Key: 41922, user_id: 41922, username: “Username2_120”, pwd: “c81e728” }

Removed: (UserVO) { Key: 41922, user_id: 41922, username: “Username2_120”, pwd: “c81e728” }

Final Collection:

AbstractCollection::__toString()
Raw VO Data (Total = 0):
Built UserVO Objects (Total = 0 of 0):

“Resetting” and Reusing a Collection

We can also replace the contents of a collection with an array of VOs or raw VO data. First we will make an ordinary array of VOs, then we will use that array to replace the contents of the $resultCollection instance.

$weakVoAry = array();
foreach($filteredCollection as $vo){
	$weakVoAry[] = $vo;
}
$resultCollection->replaceVOs($weakVoAry);
echo $resultCollection;
AbstractCollection::valid() validating #0 (Result: 1)
AbstractCollection::getRow()
Returning already built UserVO #0
AbstractCollection::valid() validating #1 (Result: 1)
AbstractCollection::getRow()
Returning already built UserVO #1
AbstractCollection::valid() validating #2 (Result: 1)
AbstractCollection::getRow()
Returning already built UserVO #2
AbstractCollection::valid() validating #3 (Result: )
AbstractCollection::replaceVOs() replacing collection data with VO data
AbstractCollection::removeAll() Emptying collection data...
Adding: (UserVO) { Key: 5, user_id: 5, username: “YourName_135”, pwd: “e4d_5” }
AbstractCollection::push() adding UserVO.5 to the collection
AbstractValueObject::toArray() converting (UserVO) { Key: 5, user_id: 5, username: “YourName_135”, pwd: “e4d_5” } to an associative array...
UserDbMap::voToArray() converting UserVO.5 back to an associative array
Adding: (UserVO) { Key: 4, user_id: 4, username: “MyName_234”, pwd: “a87_4” }
AbstractCollection::push() adding UserVO.4 to the collection
AbstractValueObject::toArray() converting (UserVO) { Key: 4, user_id: 4, username: “MyName_234”, pwd: “a87_4” } to an associative array...
UserDbMap::voToArray() converting UserVO.4 back to an associative array
Adding: (UserVO) { Key: 5, user_id: 5, username: “YourName_135”, pwd: “e4d_5” }
AbstractCollection::push() adding UserVO.5 to the collection
AbstractValueObject::toArray() converting (UserVO) { Key: 5, user_id: 5, username: “YourName_135”, pwd: “e4d_5” } to an associative array...
UserDbMap::voToArray() converting UserVO.5 back to an associative array
AbstractCollection::__toString()
Raw VO Data (Total = 3):
Raw 10: Array ( [user_id] => 5 [username] => YourName_135 [pwd] => e4d_5 )
Raw 11: Array ( [user_id] => 4 [username] => MyName_234 [pwd] => a87_4 )
Raw 12: Array ( [user_id] => 5 [username] => YourName_135 [pwd] => e4d_5 )
Built UserVO Objects (Total = 3 of 3):
VO 10: (UserVO) { Key: 5, user_id: 5, username: “YourName_135”, pwd: “e4d_5” }
VO 11: (UserVO) { Key: 4, user_id: 4, username: “MyName_234”, pwd: “a87_4” }
VO 12: (UserVO) { Key: 5, user_id: 5, username: “YourName_135”, pwd: “e4d_5” }

...and we'll empty the first collection with the built in removeAll() method

$resultCollection->removeAll();
echo $resultCollection . "<br/>";
AbstractCollection::removeAll() Emptying collection data...
AbstractCollection::__toString()
Raw VO Data (Total = 0):
Built UserVO Objects (Total = 0 of 0):

End of Script