php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #8740 $this can't be passed by reference inside of constructor
Submitted: 2001-01-16 12:51 UTC Modified: 2001-01-17 13:14 UTC
From: florian dot ortner at nme dot at Assigned:
Status: Closed Package: *General Issues
PHP Version: 4.0.4pl1 OS: Win2k
Private report: No CVE-ID: None
 [2001-01-16 12:51 UTC] florian dot ortner at nme dot at
the following code-snippet illustrated the problem:
-----
class a
{
  var $bla; // some intvalue
  var $b; // object of type b
	
  function a($i) {
    $this->bla = $i;
    // the next line ain't working in the constructor
    //$this->b = new b($this);
  }
	
  function x() {
    // the next line behaves like expected
    $this->b = new b($this);
  }

  function p() {
    echo "a::p ".$this->bla."<br>";
  }
}

class b
{
  var $a; // reference to object of type a
	
  function b(&$a) {
    $this->a = &$a;
  }
	
  function p() {
    echo "b::p ".$this->a->bla."<br>";
  }
}

$a = new a(10);
$a->x();	// works

$a->p();
$a->b->p();

$a->bla = 11;

$a->p();
$a->b->p();
-----
output:
a::p 10
b::p 10
a::p 11
b::p 11
-----
this is the wanted behaviour. var $a->b contains a object of type b which contains a reference back to object $a. object b is instanciated and assigned via the $a->x() methodcall. so far, nothing special.

when i want to get rid of the additional method in a (that's the x() method) and instanciate b inside of a's constructor and assign it to $a->b with $this, $a->b->a doesnt contain a reference to the calling a object but a copy of it.

thus, $this isn't really working inside of an object's constructor.

afaik do other languages have the same limitation of using $this inside of the constructor due to address-calculation issues.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-01-16 12:55 UTC] florian dot ortner at nme dot at
just in case, if i change a's constructor to

function a($i) {
    $this->bla = $i;
    $this->b = new b(&$this); // note the &
}

it doesn't work either.
 [2001-01-16 13:15 UTC] waldschrott@php.net
try this:

$foo =& new foobar();

 [2001-01-16 15:26 UTC] florian dot ortner at nme dot at
no, that's the opposite of what i wanna get.

$this->b = new b($this); is instancing and assigning a new object of type b to $this->b BUT not passing $this by reference to b's constructor.

$foo =& new foobar(); ofcourse doesn't change this behaviour.
 [2001-01-16 17:53 UTC] waldschrott@php.net
you did not quite understand:

1) add a &, like below
it should read, on the global scope:

$a = &new a(10);

2) remove that xtra line
$a->x();


and you'll see that it works

this is due to the fact NEW returns a copy be default

 [2001-01-17 13:14 UTC] florian dot ortner at nme dot at
works, but still a bit clumsy.

thanks for the help.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 13:01:27 2024 UTC