473,770 Members | 5,426 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_a rr = array();
} // CUserItem
function addChild($p_chi ld) {
array_push($thi s->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($item 2);
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 1323
la***********@z ipmail.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_a rr = array();
} // CUserItem
function addChild($p_chi ld) {
array_push($thi s->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($item 2);
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($valu e)
{}
}
Mar 1 '07 #2
On Mar 1, 9:38 am, Mike Roetgers <miker...@infor matik.uni-bremen.de>
wrote:
laredotorn...@z ipmail.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_a rr = array();
} // CUserItem
function addChild($p_chi ld) {
array_push($thi s->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($item 2);
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($valu e)
{}

}

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_a rr = array();
} // CUserItem
function addChild($p_chi ld) {
array_push($thi s->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($item 2);
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***********@z ipmail.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_a rr = array();
} // CUserItem
function addChild($p_chi ld) {
array_push($thi s->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($item 2);
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_a rr = array();
} // CUserItem
function addChild($p_chi ld) {
array_push($thi s->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($item 2);
$toc_items_arr[1]->print_me(); // Now it prints 1

Hope this helps.
Mar 1 '07 #5
la***********@z ipmail.com wrote:
On Mar 1, 9:38 am, Mike Roetgers <miker...@infor matik.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($item 2);
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_a rr = array();
} // CUserItem
function addChild($p_chi ld) {
array_push($thi s->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
3686
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 mentioned: I characterized one way of looking at languages in this way: a lot of them are either the agglutination of features or they're a crystallization of style. Languages such as APL, Lisp, and Smalltalk are what you might call style...
2
5618
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 those .h files into the namespace std -.h files that contain all the declarations for vector, string, etc...
5
1343
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 thought. To compound things, when dealing with inheritance 'virtuality' can only be experienced through base pointers to derived. There are ocassions though that I dont need a (particulay) 'virtual' function in base. In which case I'll have to...
9
4831
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 a winforms datagrid (must implement the IList interface). What would be a good way to create this class? 1. The CollectionBase abstract base class can be used, but it is not
8
8214
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 party COM component that takes about 5 seconds to run one of its functions (Network IO bound call). Since I dont want my GUI to freeze
9
1371
by: vidalsasoon | last post by:
Ok, this is the structure of my classes. " class Global " | " ------------------------------------------- " | | "class SomeForm1 class SomeForm2
1
1152
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. There is no
6
1446
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 actual names for discussion, I'm trying to do this with a custom log-writing class, named LogWriter.vb. So I would like to declare .. Class library project: LogWriter.vb
8
1272
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 Dog or whatever. But this is my problem: Animal X X=new Cat
0
9432
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10232
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10008
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8891
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5313
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3974
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2822
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.