Objects in arrays - functions not accessible

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
stegue
Forum Newbie
Posts: 1
Joined: Thu Jul 29, 2004 1:48 pm

Objects in arrays - functions not accessible

Post 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!
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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();
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

* echo $this->hello; ;)
Post Reply