php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #6529 Problems with $this in class constructor
Submitted: 2000-09-04 09:38 UTC Modified: 2000-11-20 09:04 UTC
From: k dot reimer at twisd dot de Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.0.2 OS: Linux
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: k dot reimer at twisd dot de
New email:
PHP Version: OS:

 

 [2000-09-04 09:38 UTC] k dot reimer at twisd dot de
Hi,

Currently we are in great trouble with PHP4. We are heavily using references 
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 have 
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=&$Child;
    }
 
    function drawRef() {
      printf(":%s:<br>\n",$this->Ref->Value);
    }
  }
 
  class CChild {
    var $Value;
 
    function CChild(&$Parent) {
      $Parent->setRef($this);
    }
  }
 
  $Parent=new CParent;
  $Child=new CChild($Parent);
  $Child->Value="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 Parents 
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 to 
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 with 
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=&$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=new CParent;
 
  // Instead I call the setRef()-method manually to do it
  $Child=new CChild;
  $Parent->setRef($Child);
 
  $Child->Value="Test";
  $Parent->drawRef();
?> 

This script is working perfectly. So I wonder why is the first script not 
working? What is wrong about making a reference to $this in the constructor 
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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-09-06 13:00 UTC] stas@php.net
reclassify
 [2000-10-31 17:55 UTC] mathieu@php.net
This is a circular reference (IIRC)
and that's not possible with PHP,
(Believe me, I learned the hard way:-))

Use your second example..

 [2000-11-20 09:04 UTC] jmoore@php.net
This should be fixed in latest CVS. (At least as much as it ever will be). Please look at the discussions on php-dev about this and reopen if you feel its still outstanding.

James
 [2003-04-29 05:00 UTC] mcp21 at cam dot ac dot uk
Have been wanting to do a similar thing; it seems to be fixed now by using a reference with the CChild constructor:
i.e.
$Child=&new CChild($Parent);
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Sun Jun 28 06:00:01 2026 UTC