I’ve got an object, let’s just say it’s a Person object with usual properties Name, Age Height etc
I’ve also got a PersonCollection class with a private property items which is type array.
When PersonCollection is constructed it gets all my people from a database and creates person objects adds them to an array.
What I want to do is loop through my PersonCollection class and print each name
“`
foreach ($PersonCollection as $Person){
echo $Person->getName();
}
Now, I have the method getName within my person Class.
When I var_dump person collection, I get this
>
object(PersonCollection)[133]
> public array ‘items’ =>
> array (size=5)
> 0 =>
> object(Person)[262]
> private int ‘id’ => int 1
> private string ‘PersonName’ => string ‘Steve’ (length=7)
> …
> array (size=4)
> …
> private ‘servername’ (dbh) => null
> private ‘username’ (dbh) => null
> private ‘password’ (dbh) => null
> private ‘dbname’ (dbh) => null
> private ‘charset’ (dbh) => null
> 1 =>
> object(Person)[266]
> private int ‘campaignId’ => int 2
> private string ‘PersonName’ => string ‘James’ (length=9)
> ….
This is my error, I suspect $Person is an array not an object?
>
Fatal error: Uncaught Error: Call to a member function getName() on array in C: