array of objects problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • julian_m

    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_o bj[] =& $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_o bj[$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

  • Jerry Stuckle

    #2
    Re: array of objects problem

    julian_m wrote:[color=blue]
    > 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_o bj[] =& $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_o bj[$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
    >[/color]


    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@attgl obal.net
    =============== ===

    Comment

    • julian_m

      #3
      Re: array of objects problem


      Jerry Stuckle wrote:
      [color=blue]
      > class ClsX{
      > var $F_name;
      > ^^^^^^
      >
      > function ClsX($name){
      > $this->Fname = $name;
      > ^^^^^
      >
      > }
      > function GetName(){
      > return $this->F_name;
      > ^^^^^^
      > }
      > }[/color]

      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

      Comment

      • Jerry Stuckle

        #4
        Re: array of objects problem

        julian_m wrote:[color=blue]
        > Jerry Stuckle wrote:
        >
        >[color=green]
        >>class ClsX{
        >> var $F_name;
        >> ^^^^^^
        >>
        >> function ClsX($name){
        >> $this->Fname = $name;
        >> ^^^^^
        >>
        >> }
        >> function GetName(){
        >> return $this->F_name;
        >> ^^^^^^
        >> }
        >>}[/color]
        >
        >
        > 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
        >[/color]

        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.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        Working...