Re: PHP 4.0 Bug #6529 Updated: Problems with $this in class constructor
| From: | Daniel J . Rodríguez | Date: | Wed, 01 Nov 2000 10:42:57 +0000 |
| Subject: | Re: PHP 4.0 Bug #6529 Updated: Problems with $this in class constructor | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-36609@lists.php.net to get a copy of this message | ||
Yes, it is not a circular reference, but I do disagree with the conclusion.
"this" is always pointing to a valid object during construction and this
same
"this" would be returned if references could be returned with the "new"
operator (which can´t be done right now). This concept is right when
talking about C++, where object creation can be aborted and exceptions
can be raised. Can this be done in PHP 4 ?. Anyway browsing through
Zend's source code shows that the only reason you get a copy of the
object is the syntax defined in "zend-parser.y" and not object construction
and validity.
I submitted but 7482, which has been "updated" by Derick,
and later a patch (zend-parser.y) to have "new" support reference return.
It is my opinion that "new" should always return references, but a valid
and reasonable alternative would be to prepend '&' to "new" to avoid
copying.
Bug 6896 which was the first one I came across having this problem
documented (though I now see 6259 was there long before), was
"suspended" with the same reason Mathieu has given for 6259:
"circular references are not supported".
Regards
Daniel
----------------------------------------------------------------------------
-
On Wed, 01 Nov 2000, you wrote:
>Is this a circular reference?
>
It _is_not_ a circular reference. The effect (I wont call it a problem/bu
g
because it is quite reasonable, just not documented ) is due to the fact
that
the object is not completely 'created' until after the constructor has
finished and accordingly $this does not reference a valid object in the
constructor. To see what really goes on check out the zend source code.
>I only see one reference so I don't see how it is a circular reference o
r am
>I just not understanding what a circular reference is?
>
>Mike
>P.S. This bug is related/duplicates (for whoever tracks these things) o
f
>bugs:
>#7511
>#7482
>#7455
>#7454
>
>-----Original Message-----
>From: mathieu@php.net [mailto:mathieu@php.net]
>Sent: Tuesday, October 31, 2000 3:55 PM
>To: php-dev@lists.php.net
>Subject: [PHP-DEV] PHP 4.0 Bug #6529 Updated: Problems with $this in
>class constructor
>
>
>ID: 6529
>Updated by: mathieu
>Reported By: k.reimer@twisd.de
>Status: Open
>Bug Type: Scripting Engine problem
>Assigned To:
>Comments:
>
>This is a circular reference (IIRC)
>and that's not possible with PHP,
>(Believe me, I learned the hard way:-))
>
>Use your second example..
>
>
>Previous Comments:
>------------------------------------------------------------------------
---
>
>[2000-09-06 13:00:05] stas@php.net
>reclassify
>
>------------------------------------------------------------------------
---
>
>[2000-09-04 09:38:57] k.reimer@twisd.de
>Hi,
>
>Currently we are in great trouble with PHP4. We are heavily using refere
nces
>to objects in our current project and we need to create a reference from
a
>class in the constructor of the class itself. But it is not working. I h
ave
>tested it with PHP 4.0.1 and the new PHP 4.0.2.
>Finally I managed to write a short example script where it is not
>working. Here is the code that is refusing to work correctly:
>
><?php
> class CParent {
> var $Ref;
>
> function setRef(&$Child) {
> $this->Ref=3D&$Child;
> }
>
> function drawRef() {
> printf(":%s:<br>n",$this->Ref->Value);
> }
> }
>
> class CChild {
> var $Value;
>
> function CChild(&$Parent) {
> $Parent->setRef($this);
> }
> }
>
> $Parent=3Dnew CParent;
> $Child=3Dnew CChild($Parent);
> $Child->Value=3D"Test";
> $Parent->drawRef();
>?>
>
>This script creates a Parent-Class and a Child-Class. The constructor of
the
>child class connects itself to the Parent class by passing its reference
to
>the Parent. Then I change its property $Value to "Test" and call the Par
ents
>drawRef()-function. The drawRef-Function is printing the Value of the
>referenced object. Well, finally it is NOT printing the Value. The value
>seems to be empty. But the reference is working. If a add some methods t
o
>CChild, the Parent can call these methods through the reference. But all
>properties of CChild are empty. It looks like $Parent is working with a
COPY
>of $Child and not with a reference. I thought there is something wrong w
ith
>the setRef()-method, where I store the Reference in the $Ref-Variable,
>but...
>
>.....if I do the same in another way (Not using the constructor, instead
>writing the reference from Child to Parent manually) it is working. Here
is
>the working code:
>
><?php
> class CParent {
> var $Ref;
>
> function setRef(&$Child) {
> $this->Ref=3D&$Child;
> }
>
> function drawRef() {
> printf(":%s:<br>n",$this->Ref->Value);
> }
> }
>
> class CChild {
> var $Value;
>
> // In this example I don't use the constructor to connect this
> // class to CParent
> }
>
> $Parent=3Dnew CParent;
>
> // Instead I call the setRef()-method manually to do it
> $Child=3Dnew CChild;
> $Parent->setRef($Child);
>
> $Child->Value=3D"Test";
> $Parent->drawRef();
>?>
>
>This script is working perfectly. So I wonder why is the first script no
t
>working? What is wrong about making a reference to $this in the construc
tor
>of $this? And to create just more confusion: If I assign a value to
>$this->Value in the constructor, this value is printed by drawRef() and
>values assigned later are ignored.
>
>------------------------------------------------------------------------
---
>
>
>Full Bug description available at:
>http://bugs.php.net/?id=3D6529
>
>
--
Regards,
Alan van den Bosch
Sanguis Pty Ltd
/* All generalizations are false. */
--
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, e-mail: php-dev-unsubscribe@lists.php.net
For additional commands, e-mail: php-dev-help@lists.php.net
To contact the list administrators, e-mail: php-list-admin@lists.php.net