note 61390 added to ref.classobj
| From: | Wolverine at osu1 dot php dot net | Date: | Thu, 02 Feb 2006 06:07:51 +0000 |
| Subject: | note 61390 added to ref.classobj | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-102858@lists.php.net to get a copy of this message | ||
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?
----
Server IP: 195.136.184.34
Probable Submitter: 213.25.213.102
----
Manual Page -- http://www.php.net/manual/en/ref.classobj.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+61390
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61390&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61390&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61390&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61390&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61390&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61390&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61390&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+61390&report=yes
Search -- http://master.php.net/manage/user-notes.php