Confusion about classes and objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • laredotornado@zipmail.com

    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

  • Mike Roetgers

    #2
    Re: Confusion about classes and objects

    laredotornado@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)
    {}
    }

    Comment

    • laredotornado@zipmail.com

      #3
      Re: Confusion about classes and objects

      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. -

      Comment

      • =?ISO-8859-15?Q?Iv=E1n_S=E1nchez_Ortega?=

        #4
        Re: Confusion about classes and objects

        $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.

        Comment

        • Mark Babli

          #5
          Re: Confusion about classes and objects

          laredotornado@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.

          Comment

          • Hendri Kurniawan

            #6
            Re: Confusion about classes and objects

            laredotornado@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

            Comment

            • Toby A Inkster

              #7
              Re: Confusion about classes and objects

              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!

              Comment

              Working...