note 61390 rejected from ref.classobj by nlopess
| From: | nlopess@php.net | Date: | Thu, 02 Feb 2006 19:51:56 +0000 |
| Subject: | note 61390 rejected from ref.classobj by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-102886@lists.php.net to get a copy of this message | ||
Note Submitter: Wolverine
----
I have something like this:
<?php
class A
{
var $ar;
function A()
{
$ar=array();
}
}
class B
{
var $foo;
function B($in_ar)
{
$this->foo=$in_ar;
}
function showFoo()
{
return $this->foo;
}
}
class C
{
var $bar;
var $gee;
function C()
{
$this->bar=new A();
$this->gee=new B($this->bar);
}
function register($name)
{
array_push($this->bar->ar,$name);
}
}
$whiz=new C();
$whiz->register('dummy');
print_r($whiz->bar->ar); // This wil return array with one element "dummy"
print_r($whiz->gee->showFoo()); // This will return empty array
?>
Is there any way so that $whiz->gee->foo will also change when doing
$whiz->register('dummy')?
PHP should pass objects by reference (particularly object of class A should be the same instance in
object of class B and C), but somehow it doesn't. It creates two instances of object A instead
of one. Does anyone know how to do it properly in later than PHP4.2?