Posted by Jerry Stuckle on 01/20/06 07:29
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.
jstucklex@attglobal.net
==================
Navigation:
[Reply to this message]
|