|
Posted by Hendri Kurniawan on 03/01/07 21:35
laredotornado@zipmail.com wrote:
> On Mar 1, 9:38 am, Mike Roetgers <miker...@informatik.uni-bremen.de>
> wrote:
>> laredotorn...@zipmail.com schrieb:
>>
>>> Hi,
>>> Using PHP 4.4.4, I have a class defined like so
>>> $item = new CUserItem(1);
>>> $item2 = new CUserItem(2);
>>> $toc_items_arr[1] = $item;
>>> $item->addChild($item2);
>>> print $toc_items_arr[1]-
>>>> numChildren(); // still prints zero!
>>> The "print" line should print out "1" because I have added a child.
>>> But it does not. What's going wrong? - Dave
>> I don't see a constructor in your class. When you're working with PHP4,
>> you must name the constructor like the class:
>
> class CUserItem {
> var $m_id;
> var $m_children_arr;
> function CUserItem($p_id)
> {
> $this->m_id = $p_id;
> $this->m_children_arr = array();
> } // CUserItem
> function addChild($p_child) {
> array_push($this->m_children_arr, $p_child);
> } // addChild
> function numChildren() {
> return count($this->m_children_arr);
> } // numChildren
> } // CUserItem
>
> Please consider this class in the question posed above. -
>
You are using PHP4, threfore when assigning object to a variable,
you should use an assign reference (ie. =&)
So instead of:
$toc_items_arr[1] = $item;
use:
$toc_items_arr[1] =& $item;
This makes sure that the line:
print $toc_items_arr[1]->numChildren();
refers to the one and original $item
Hendri Kurniawan
[Back to original message]
|