Re: PHP 4.0 Bug #8935 Updated: A reference to 'this' can not be used in the constructor method for a class.
| From: | Jani Taskinen | Date: | Sat, 10 Mar 2001 00:12:45 +0000 |
| Subject: | Re: PHP 4.0 Bug #8935 Updated: A reference to 'this' can not be used in the constructor method for a class. | ||
| References: | 1 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-48460@lists.php.net to get a copy of this message | ||
Uhm..I have always closed the duplicates after the original (which they
refer to) was closed. Any opinions about this? As the current bug system
doesn't have any support for 'multiple' closing, I think it's better to
close bugs after they are fixed, be it a duplicate or not.
--Jani
On 9 Mar 2001 andre@php.net wrote:
>ID: 8935
>Updated by: andre
>Reported By: johanp@aditus.nu
>Old-Status: Closed
>Status: Duplicate
>Bug Type: Class/Object related
>Assigned To:
>Comments:
>
>not really this is only a duplicate report for an already
>fixed bug...
>
>Previous Comments:
>---------------------------------------------------------------------------
>
>[2001-03-09 17:09:08] sniper@php.net
>It's fixed -> closed.
>
>--Jani
>
>
>---------------------------------------------------------------------------
>
>[2001-03-09 13:50:45] andre@php.net
>this has been fixed for months,
>please use =& new instead of = new in this special case
>
>---------------------------------------------------------------------------
>
>[2001-02-24 14:14:09] jmoore@php.net
>Please see the 4.0.4 release change log a lot of things along this line were fixed.
>
>James
>
>---------------------------------------------------------------------------
>
>[2001-01-26 09:37:16] johanp@aditus.nu
>The following script demonstrates the problem.
>
><?php
>/*
>thistest.php
>jp, 2001-01-25
>Demonstration of bug in passing '$this' as a reference in the constructor.
>The problem is that it will not be a reference to the newly created object that is passed but
>rather a new copy. This is demonstrated below.
>
>The Container class is just a driver class
>
>The bug is demostrated in the A1 class. The A class uses an Init() method to pass the
>'$this' reference which is a workaround for this particular bug.
>
>Analysis of problem:
>It seems that the '$this' pointer is not safe to use in the constructor since it
>probably doesn't get properly initialized for the object until after the constructor has been
>run.
>
>*/
>
>
>
>class ContainerA {
> var $a;
> function ContainerA() {
> $this->a=new A();
> $this->a->Init();
> echo "A val before calling change:
> ".$this->a->val."<br>";
> $this->a->bobj->ChangeA();
> echo "A val after calling change (should be 3):
> ".$this->a->val."<br>";
> }
>}
>
>class A {
> var $val=1;
> var $bobj=null;
>
> function A() {
> $this->bobj = new B();
> }
> function Init() {
> // Workaround it is safe to use a refernce to this outside the constuctor
> $this->bobj->Init(&$this);
> }
> function SetVal($v) {
> $this->val=$v;
> }
>};
>
>class B {
> var $aobj=null;
> function B() {
> }
> function Init(&$obj) {
> $this->aobj=&$obj;
> }
> function ChangeA() {
> $this->aobj->SetVal(3);
> }
>};
>
>class ContainerA1 {
> var $a;
> function ContainerA1() {
> $this->a=new A1();
> echo "A val before calling change:
> ".$this->a->val."<br>";
> $this->a->bobj->ChangeA();
> echo "A val after calling change (should be 3):
> ".$this->a->val."<br>";
> }
>}
>
>// BUG
>class A1 {
> var $val=1;
> var $bobj=null;
>
> function A1() {
> $this->bobj = new B();
>
> // BUG. A reference of 'this' is NOT passed here as it seems but a copy!
> // Hence the bobj will contain another copy of A1 and not the one we
> // are just creating.
> $this->bobj->Init(&$this);
> }
> function SetVal($v) {
> $this->val=$v;
> }
>};
>
>echo "Demonstration of 'this' bug.<p>";
>echo "Using class A (workaround)<br>";
>$c1=new ContainerA();
>
>echo "<p>Using class A1 (BUG)<br>";
>$c1=new ContainerA1();
>
>?>
>
>---------------------------------------------------------------------------
>
>
>
>ATTENTION! Do NOT reply to this email!
>To reply, use the web interface found at
>http://bugs.php.net/?id=8935&edit=2
>
>
>