Page 1 of 1

Accessing methods of an array of objects

Posted: Thu May 20, 2004 1:06 pm
by cosmicstring
I have a class, that for one of its properties has an array.

In that array I am storing a number of other objects.

I can loop through the array of objects no problem using foreach, but when I try to access a method of one of the objects it gives me a "Call to a member function on a non-object in" error.

Any ideas how I access an object that's been stored in an array?

Thanks

Posted: Thu May 20, 2004 1:37 pm
by launchcode
Depends on your code, but the following works for me:

Code: Select all

<?php
class ObjectA
{
	function test ()
	{
		echo "Hello from Object A";
	}
}

class ObjectB
{
	function test ()
	{
		echo "Hello from Object B";
	}
}

class ObjectC
{
	function test ()
	{
		echo "Hello from Object C";
	}
}

$object_array[] = new ObjectA();
$object_array[] = new ObjectB();
$object_array[] = new ObjectC();

$object_array[1]->test();
?>