I’m following the tutorial “[URL=“http://www.kirupa.com/developer/php/php_oop_intro_pg3.htm”]Introduction to OOP using PHP” and I’m stuck the third script on Page 3. I made a revision to check and see if I got the hang of it by using an data array of three names instead of mysql. However when I run the script listed below I do not see the expected print out of the names. Please advise.
<?
/* instantiate group */
$group = new UsersGroup;
/* loop through our group, echo user names */
foreach( $group->getUsers() as $user ) {
echo $user->getName().'<br />';
}
/* User class, same as before */
class User {
private $name;
function __construct( $attribs ) {
$this->name = $attribs['name'];
}
/* name methods */
function setName( $val ) {
$this->name = $val;
return;
}
function getName() {
return $this->name;
}
}
/* Contains a group of User objects */
class UsersGroup {
private $name; // name of group
private $group = array(); // group of User objects
function __construct() {
/* Query */
$result = array('David','Bob','Jerry');
/* Adds user to group with each row of data */
foreach ($result as $uname) {
$this->addUser( $uname );
}
}
/*
Add a user to Group
Does simple check to see if we pass an array (like $attribs)
or if we pass an object (like a User object)
*/
function addUser( $user ) {
if( is_object( $user ) ) {
array_push( $this->group, $user );
}
if( is_array( $user ) ) {
$noob = new User( $user );
array_push( $this->group, $noob );
}
return;
}
/* Returns the group (which is an array) */
function getUsers() {
return $this->group;
}
}
?>