arrays of objects and methods.

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
User avatar
untouchable
Forum Newbie
Posts: 7
Joined: Sat Jul 06, 2002 11:41 pm

arrays of objects and methods.

Post by untouchable »

Code: Select all

<?
class testClass
&#123;
    var $a;
    
    function printa()
    &#123;
        echo $this->a;
    &#125;
&#125;

$obj&#1111;10] = new testClass;
$obj&#1111;0]->a = 4;
$obj&#1111;0]->printa();
?>
this does not work. at all. I'm not sure why but it sure as hell is frusturating.

the method printa will go as undefined but if you wish to do the following

Code: Select all

<?=$obj&#1111;0]->a?>
or any form of echo, there is no problem. why does this happen, am i missing something?
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Re: arrays of objects and methods.

Post by protokol »

untouchable wrote:

Code: Select all

<?
class testClass
&#123;
    var $a;
    
    function printa()
    &#123;
        echo $this->a;
    &#125;
&#125;

$obj&#1111;10] = new testClass;
$obj&#1111;0]->a = 4;
$obj&#1111;0]->printa();
?>
this does not work. at all. I'm not sure why but it sure as hell is frusturating.

the method printa will go as undefined but if you wish to do the following

Code: Select all

<?=$obj&#1111;0]->a?>
or any form of echo, there is no problem. why does this happen, am i missing something?
The line $obj[10] = new testClass; means that you are instantiating an instance of the testClass and placing it into index 10 in the $obj array. So, when you do $obj[0]->whatever, it's not going to work because you did not specify an instance of the class in index 0 of $obj.

Make $obj[10] be $obj[0] and you'll see that your next lines will work.
User avatar
untouchable
Forum Newbie
Posts: 7
Joined: Sat Jul 06, 2002 11:41 pm

Post by untouchable »

so how is it that i can print properties but not call methods?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You're not printing a property but trying to set one. You should take a look into the error.log of your server ;)
Post Reply