# Trouble with objects and arrays

**URL:** <https://forum.kirupa.com/t/trouble-with-objects-and-arrays/310839>\
**Category:** programming\
**Created:** [June 21, 2010, 3:31am UTC](https://forum.kirupa.com/t/trouble-with-objects-and-arrays/310839 "2010-06-21T03:31:18Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ShyneRocc](https://avatars.discourse-cdn.com/v4/letter/s/bbce88/32.png) [@ShyneRocc](https://forum.kirupa.com/u/ShyneRocc)\
**Post date:** [June 21, 2010, 3:31am UTC](https://forum.kirupa.com/t/trouble-with-objects-and-arrays/310839/1 "2010-06-21T03:31:18Z")

</div>

I’m following the tutorial [“[URL=“http://www.kirupa.com/developer/php/php\_oop\_intro\_pg3.htm”] **Introduction** to OOP using PHP”](http://www.kirupa.com/developer/php/php_oop_intro_pg3.htm) 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.

```php
<?
/* 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;
    }
}

?>

```
