You are here: Re: Confusion about classes and objects « PHP Programming Language « IT news, forums, messages
Re: Confusion about classes and objects

Posted by Mark Babli on 03/01/07 17:12

laredotornado@zipmail.com wrote:
> Hi,
>
> Using PHP 4.4.4, I have a class defined like so
>
> class CUserItem {
> var $m_id;
> var $m_children_arr;
> function CUserTOCItem($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
>
> then in a separate bit of code, I have an associative array of
> objects, with the key being the id and the value being the object.
> Unfortunately, when I try and manipulate the objects in the array, it
> doesn't take ...
>
> $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
>
Hello,

The error message I got was: "Fatal Error: Object of type CUserItem
could not be converted into a string". The modified code works.


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

// ADDED this function to print the identity
function print_me(){
print $this->m_id;
} // added this new function
} // CUserItem


$item = new CUserItem(1);
$item2 = new CUserItem(2);
$toc_items_arr[1] = $item;
$item->addChild($item2);
$toc_items_arr[1]->print_me(); // Now it prints 1

Hope this helps.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация