By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,949 Members | 1,838 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,949 IT Pros & Developers. It's quick & easy.

Confusion about classes and objects

P: n/a
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

Mar 1 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
la***********@zipmail.com schrieb:
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
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 {
function CUserItem($value)
{}
}
Mar 1 '07 #2

P: n/a
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
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

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 {
function CUserItem($value)
{}

}

Excuse me, I rewrote my code to make a little more sense on the
newsgroup but forgot to change a couple of things. here is the classs

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. -

Mar 1 '07 #3

P: n/a
$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
You're assigning a copy og $item to $toc_items_arr[1], not a reference to
$item.

Thus, you are updating $item but not $toc_items_arr[1].

Please re-read the chapter on references in the PHP manual; after you've
done so, you'll probably understand what's going on. And then, by RTFM,
you'll learn to assing things by reference.

--
----------------------------------
IvŠn SŠnchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Un ordenador no es un televisor ni un microondas, es una herramienta
compleja.
Mar 1 '07 #4

P: n/a
la***********@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.
Mar 1 '07 #5

P: n/a
la***********@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
Mar 1 '07 #6

P: n/a
Iv√°n S√°nchez Ortega wrote:
You're assigning a copy og $item to $toc_items_arr[1], not a reference to
$item.
This is almost certainly the problem.

Note that in PHP 5 the default behaviour of the assignment operator is
changed with respect to how it operates on objects, and is closer to the
Java behaviour which is what I think you were expecting.

PHP 4:

<?php
$a = new Object();
$b = $a;
// Variables $a and $b point to two different objects.
// Changes to $a do not effect $b.
?>

PHP 5:

<?php
$a = new Object();
$b = $a;
// Variables $a and $b point to the same object.
// Changes to $a effect $b.
?>

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Mar 2 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.