RE: [PHP4BETA] Re: [PHP3] Re: [PHP4BETA] Re: [PHP3] OO
| From: | Zeev Suraski | Date: | Tue, 10 Aug 1999 16:15:26 +0000 |
| Subject: | RE: [PHP4BETA] Re: [PHP3] Re: [PHP4BETA] Re: [PHP3] OO | ||
| References: | 1 | Groups: | php.version4 |
| Request: | Send a blank email to php-version4+get-3290@lists.php.net to get a copy of this message | ||
At 15:54 10/08/99 , Samuel Liddicott wrote:
-----Original Message----- From: Zeev Suraski [mailto:zeev@zend.com] Sent: 05 August 1999 06:20 AM To: Manuel Lemos Cc: php3@lists.php.net; sander@3dnews.net; php4beta@lists.php.net Subject: [PHP4BETA] Re: [PHP3] Re: [PHP4BETA] Re: [PHP3] OO Not really. The problem doesn't occur when you have multiple variables pointing at the same zval (pval) value - in that case, we can easily rely on Zend's reference counting to call the destructor only when the last reference is deleted. The problem occurs when there are actually multiple zval's that contain the data of the same object (they contain the class type and the properties - but changing one will not change the other, and, they don't have their resources doubled - they point at the same resources (e.g. file handles)). How could this occur?You have to know the internals of Zend and PHP to understand that, but I'll give you a sketch. $a = new object;
$b = $a; // $a and $b now point to the same place in memory, but if one of them is changed,
// they're separated and no longer point to the same memory, so that changes made to
// $a won't be applied to $b and vice versa
// lets say the script ends here, and $a and $b are destroyed. Lets say $a holds a resource, be it
// an open file, an open transaction handle, or whatever else you can think of. You would obviously
// not want to deallocate the resource more than once, so it's obvious the destructor shouldn't run
// twice on both $a and $b, but just once on their related object
// But, what if the resource $a holds is no longer the same as the resource $b holds? What if
// you changed the resource $b holds through one of its methods or even from the outside world?
// Then you would want the destructor to run twice, on both $a and $b
The inherent problem with adding destructors to PHP results from:
1. Objects aren't always allocated with 'new', but can be copied around using standard assignments
2. There's no copy constructor that would duplicate resources
3. There is no way to know when, and how many times the destructor should be run, and with what value as $this.
To change this, we would have to make it impossible to copy around objects, including sending them to functions, etc, except for copying by-reference. All object instanciations would have to be done using 'new'. This is a *major* downwards incompatibility, and extremely inefficient to implement since it would require a lot of run-time work to determine what kind of assignments should be conducted every time.
Zeev
--
Zeev Suraski <zeev@zend.com> http://www.zend.com/
For a PGP public key, finger bourbon@netvision.net.il