Re: Object and reference problem

From: Date: Thu, 18 Oct 2001 09:29:37 +0000
Subject: Re: Object and reference problem
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-71464@lists.php.net to get a copy of this message
Yes, thanks, but then I can't pass a string or int as a parameter as well, which is what a want. Neil Kimber wrote:
You're not passing by reference. Change:
    function add($child) {
       $this->children[] = $child;
    }
to
    function add(&$child) {
       $this->children[] = &$child;
    }
This should work. When you pass an object as a parameter PHP makes a copy of that object as a local instance in your method. So, your original code: 4 $obj2->add(", something else"); 5 $obj1->add($obj2); would alter $obj2 and store a copy of that altered object in $obj1. But, 5 $obj1->add($obj2); 4 $obj2->add(", something else"); would store a copy of unaltered $obj2 in $obj1. You'd then alter a different instance of $obj2 in the succeeding line. By using the suggested altered code at the top of this email, line 5 would not pass a copy but would pass a reference to the actual instance of the object. Note, you also have to ensure that you store a reference to this guy in your array as by default adding items to arrays causes copies to be made. So you also need the line:
       $this->children[] = &$child;
Hope this helps.
-----Original Message----- From: Brian White [mailto:bwhite@steptwo.com.au] Sent: 17 October 2001 04:41 To: SafeV; php-general@lists.php.net Subject: Re: [PHP] Object and reference problem I think you are going to have to live with it. Functional overloading only really makes sense in strongly typed languages, and I bet is a bugger to implement in a name based scripting language so I don't think you are goign to get it. What I have sometimes wished for was that all objects and arrays were passed around as pointers by default, with the ability to create a copy when required, but that is such a fundamental change that I doubt if it will happen, and there are probably goo reasons not to ( have I ever told people how much I like Python ... ) Maybe you are just going to have to bite the bullet and have an "AddRef" method...... At 04:28 17/10/2001 +0200, SafeV wrote:
Someone, have a go at this! I have encountered a very troublesome problem, which I don't see how to solve in an elegant way, since PHP doesn't support method overloading. Suppose my class is, kind of a tree, like: class A { var $children; function A() {
      $this->children = array();
} // I want this to accept both objects and strings etc. // IE. mixed function add($child) {
     $this->children[] = $child;
} function printStuff() {
     foreach ($this->children as $child) {
        if (is_object($child))
           $child->printStuff();
        else
          echo $child;
     }
} } Now this is fine: 1 $obj1 = new A(); 2 $obj2 = new A(); 3 $obj1->add("Something"); 4 $obj2->add(", something else"); 5 $obj1->add($obj2); 6 $obj1->printStuff(); and prints out "something, something else" But if I swap lines 4 and 5 it only prints out "something". Obviously, the add method of $obj1 only receives a copy of $obj2, and if I try to add something to $obj2 afterwards, even thought it's
supposed
to be hooked with $obj1, it will "disappear". For objects only I could solve this with declaring add() like: function add(&$child) { $this->children[] = $child; } So it receives a reference of the object, but then I can't add a string (fatal error, cannot pass string as reference), or an integer or double, for that matter! And I really don't want to use different method names! I don't have full control over the reference stuff in PHP, does anyone know how I can fix this? Or a workaround? Thanx! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net For additional commands, e-mail: php-general-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net
------------------------- Brian White Step Two Designs Pty Ltd - SGML, XML & HTML Consultancy Phone: +612-93197901 Web: http://www.steptwo.com.au/ Email: bwhite@steptwo.com.au -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net For additional commands, e-mail: php-general-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net


« previous php.general (#71464) next »