473,513 Members | 4,753 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Confusion about classes and objects

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
6 1313
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
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
$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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
3638
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages...
2
5599
by: puzzlecracker | last post by:
after reading some of the post I found out something rather radical to my previous understanding: that when you do #include<iostream> #include<string> #include<vector> etc compiler puts...
5
1329
by: ma740988 | last post by:
Prefer composition to inheritance (can't recall which text I stole that line from) is one of the fundamental tenets thats engrained in my mind. Having said that inheritance requires careful...
9
4818
by: craig | last post by:
Assume that you would like to create a custom collection class that is: 1. Strongly-typed (only holds Customer objects) 2. Read-only (user cannot add Customer objects) 3. Able to be bound to...
8
8172
by: Z D | last post by:
Hello, I'm having a strange problem that is probably due to my lack of understanding of how threading & COM Interop works in a WinForms.NET application. Here's the situation: I have a 3rd...
9
1354
by: vidalsasoon | last post by:
Ok, this is the structure of my classes. " class Global " | " ------------------------------------------- " | ...
1
1143
by: Epetruk | last post by:
Hello, I have a solution with two projects. One of the projects is called MyProj with a root namespace called MyProj.Obj. The single source (vb) file for MyProj has a class called Obj....
6
1437
by: Grok | last post by:
In writing a class library, one of the classes should only ever be instantiated once, and its object should be accessible to every other class in the library. How can I do that? To provide some...
8
1257
by: Bart C | last post by:
Have just started looking at C++, and tutorials about virtual functions have thrown up some confusing issues. These always seem to use example classes such as Animal and derived classes Cat and...
0
7254
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7094
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7519
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5079
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.