php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #26794 $that not available in parent class
Submitted: 2004-01-05 09:17 UTC Modified: 2004-02-03 13:43 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: alex_boyer at hotmail dot com Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 5CVS-2004-01-11 OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
36 + 49 = ?
Subscribe to this entry?

 
 [2004-01-05 09:17 UTC] alex_boyer at hotmail dot com
Description:
------------
$that is not available in parent class B (uncomment code in class A you will see it). So it is not possible to clone  class in a recursive way.



Reproduce code:
---------------
class A {
    public $a;
    function __clone(){
       //if( $that === null ) die('$that is null');
       $this->a = $that->a;
    }
}
class B extends A {
   public $b;   
   function __clone(){
      parent::__clone();
      $this->b = $that->b;
   }
}
$b = new B();
$b->a = 'AAA';
$b->b = 'BBB';
$clone =  $b->__clone();
print_r($clone);

Expected result:
----------------
B Object
(
    [b] => BBB
    [a] => AAA
)

Actual result:
--------------
B Object
(
    [b] => BBB
    [a] =>
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-01-05 09:20 UTC] alex_boyer at hotmail dot com
i mean that $that is not available in parent class A (but IT IS available in class B)
 [2004-02-03 13:43 UTC] andi@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

The mechanism of __clone() was changed to solve such problems.
To clone an object you should now use clone $obj;
It will create a replica of $obj and call $replica->__clone() if available. No more need for $that because you have all the info replicated already
Calling the parent __clone() can be done by doing parent::__clone()
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 23:01:34 2024 UTC