473,388 Members | 1,198 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

array of objects problem

I'm having problems calling methods inside an array of objects. It
seems that I can't understand very well how "&" works

The code which follows, prints out

ClsSet Constructor
El nombre del objeto es:
El nombre del objeto es:
El nombre del objeto es:

whereas I expected

ClsSet Constructor
El nombre del objeto es: cls1
El nombre del objeto es: cls2
El nombre del objeto es: cls3

Where i'm wrong?


class ClsX{
var $F_name;

function ClsX($name){
$this->Fname = $name;
}
function GetName(){
return $this->F_name;
}
}//class

class ClsSet{
//comentarios de clase;
var $F_array_of_obj = array();

function ClsSet(){
echo "ClsSet Constructor<br>";
}
function AddObj(&$obj){
$this->F_array_of_obj[] =& $obj;
}

function Names(&$obj){
//print_r($this->F_array_of_obj);
for ($i = 0; $i <= (count($this->F_array_of_obj)-1); $i++){
//print_r($object);
$object =& $this->F_array_of_obj[$i]; //I've tryied with and
without &
echo "El nombre del objeto es: ".$object->GetName().'<br>'; //It
actually doesn't call GetName()
}
}
}//class
$clsSet = new ClsSet;

$cls1 = new ClsX("cls1");
$cls2 = new ClsX("cls2");
$cls3 = new ClsX("cls3");

$clsSet->AddObj($cls1);
$clsSet->AddObj($cls2);
$clsSet->AddObj($cls3);

$clsSet->Names();
regards - jm

Jan 20 '06 #1
3 1136
julian_m wrote:
I'm having problems calling methods inside an array of objects. It
seems that I can't understand very well how "&" works

The code which follows, prints out

ClsSet Constructor
El nombre del objeto es:
El nombre del objeto es:
El nombre del objeto es:

whereas I expected

ClsSet Constructor
El nombre del objeto es: cls1
El nombre del objeto es: cls2
El nombre del objeto es: cls3

Where i'm wrong?


class ClsX{
var $F_name;

function ClsX($name){
$this->Fname = $name;
}
function GetName(){
return $this->F_name;
}
}//class

class ClsSet{
//comentarios de clase;
var $F_array_of_obj = array();

function ClsSet(){
echo "ClsSet Constructor<br>";
}
function AddObj(&$obj){
$this->F_array_of_obj[] =& $obj;
}

function Names(&$obj){
//print_r($this->F_array_of_obj);
for ($i = 0; $i <= (count($this->F_array_of_obj)-1); $i++){
//print_r($object);
$object =& $this->F_array_of_obj[$i]; //I've tryied with and
without &
echo "El nombre del objeto es: ".$object->GetName().'<br>'; //It
actually doesn't call GetName()
}
}
}//class
$clsSet = new ClsSet;

$cls1 = new ClsX("cls1");
$cls2 = new ClsX("cls2");
$cls3 = new ClsX("cls3");

$clsSet->AddObj($cls1);
$clsSet->AddObj($cls2);
$clsSet->AddObj($cls3);

$clsSet->Names();
regards - jm

class ClsX{
var $F_name;
^^^^^^

function ClsX($name){
$this->Fname = $name;
^^^^^

}
function GetName(){
return $this->F_name;
^^^^^^
}
}

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jan 20 '06 #2

Jerry Stuckle wrote:
class ClsX{
var $F_name;
^^^^^^

function ClsX($name){
$this->Fname = $name;
^^^^^

}
function GetName(){
return $this->F_name;
^^^^^^
}
}


Ohh! what a silly mistake...
Thanks Jerry. BTW, What debugger are you using? I'm using PHP designer
2006 beta, which being a nice editor, has not the option to put breaks
and things like that, nor even is able to detect such a silly errors
like the one I had...

regards - jm

Jan 20 '06 #3
julian_m wrote:
Jerry Stuckle wrote:

class ClsX{
var $F_name;
^^^^^^

function ClsX($name){
$this->Fname = $name;
^^^^^

}
function GetName(){
return $this->F_name;
^^^^^^
}
}

Ohh! what a silly mistake...
Thanks Jerry. BTW, What debugger are you using? I'm using PHP designer
2006 beta, which being a nice editor, has not the option to put breaks
and things like that, nor even is able to detect such a silly errors
like the one I had...

regards - jm


Notepad. :-)

Seriously, I don't use a debugger. I tried Zend and loved it, but not
enough to pay for it. I haven't found a "free" one I like well yet.

I just interspace echo, print_r, etc. when I need to do some debugging.

It didn't take long to find your problem, though, just by looking at the
code. It's something I've done too may times myself :-).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jan 20 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
7
by: Robert Mark Bram | last post by:
Hi All! How do you get the length of an associative array? var my_cars= new Array() my_cars="Mustang"; my_cars="Station Wagon"; my_cars="SUV"; alert(my_cars.length);
19
by: Tom Jastrzebski | last post by:
Hello, I was just testing VB.Net on Framework.Net 2.0 performance when I run into the this problem. This trivial code attached below executed hundreds, if not thousand times faster in VB 6.0...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
3
by: sk.rasheedfarhan | last post by:
Hi , Here I am new user to C#, my problem is I have to use dynamic Array of objects. But I heard C# don't support ptrs (using managed code C# support). In short i initialized objects of 1000...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
5
by: Pukeko | last post by:
Hi, this is an array that is used for a dropdown menu. var imageArray = new Array( "ecwp://" + document.location.host + "/massey/images/massey/ massey_07_fullres.ecw", "ecwp://" +...
25
by: biplab | last post by:
Hi all, I am using TC 3.0..there if I declare a integer array with dimension 162*219...an error msg saying that too long array is shown....what should I do to recover from this problem???
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.