Page 1 of 1

Objects in arrays - functions not accessible

Posted: Thu Jul 29, 2004 1:48 pm
by stegue
I can't seem to access a class function when my objects are stored in arrays? I am new to PHP but not to OOP.

Example:

class foo
{
var $a;
var $b;
function f1() {
return "f1 function return";
}
}

I use the class in another php file...

$arrayOfFoos[3]=new foo;
$arrayOfFoos[3]->a="the value of variable a for this object";

I then can use the variables values in this object...

print $arrayOfFoos[3]->a;

... which returns "the value of variable a for this object"

HOWEVER... when I call a function...

print $arrayOfFoos[3]->f1();

...I get "function not accessible".

In the SAME PHP I can use/access the function just fine if I do not put the object into an array. eg...

$ttt = new foo;
print $ttt->f1();

... works.

HELP??? Can objects functions be accessed if the object is stored in an array?? HOW??

THANKS!

Posted: Thu Jul 29, 2004 3:02 pm
by kettle_drum
Yeah i do it all the time except i use named indexes in the array instead of just the indices.

Code: Select all

class foo {
   var $hello = "boo";

   function bar(){
      echo $hello;
   }
}

$objects - array();

$objects['foo'] - new foo();
$objects[foo']->bar();

Posted: Thu Jul 29, 2004 5:51 pm
by feyd
* echo $this->hello; ;)