note 95681 added to language.references.whatdo
| From: | Amaroq at osu1 dot php dot net | Date: | Fri, 15 Jan 2010 14:08:10 +0000 |
| Subject: | note 95681 added to language.references.whatdo | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-165004@lists.php.net to get a copy of this message | ||
When using references in a class, you can reference $this-> variables.
<?php
class reftest
{
public $a = 1;
public $c = 1;
public function reftest()
{
$b =& $this->a;
$b = 2;
}
public function reftest2()
{
$d =& $this->c;
$d++;
}
}
$reference = new reftest();
$reference->reftest();
$reference->reftest2();
echo $reference->a; //Echoes 2.
echo $reference->c; //Echoes 2.
?>
However, this doesn't appear to be completely trustworthy. In some cases, it can act strangely.
<?php
class reftest
{
public $a = 1;
public $c = 1;
public function reftest()
{
$b =& $this->a;
$b++;
}
public function reftest2()
{
$d =& $this->c;
$d++;
}
}
$reference = new reftest();
$reference->reftest();
$reference->reftest2();
echo $reference->a; //Echoes 3.
echo $reference->c; //Echoes 2.
?>
In this second code block, I've changed reftest() so that $b increments instead of just gets
changed to 2. Somehow, it winds up equaling 3 instead of 2 as it should.
----
Server IP: 69.147.83.197
Probable Submitter: 24.16.127.219
----
Manual Page -- http://www.php.net/manual/en/language.references.whatdo.php
Edit -- https://master.php.net/note/edit/95681
Del: integrated -- https://master.php.net/note/delete/95681/integrated
Del: useless -- https://master.php.net/note/delete/95681/useless
Del: bad code -- https://master.php.net/note/delete/95681/bad+code
Del: spam -- https://master.php.net/note/delete/95681/spam
Del: non-english -- https://master.php.net/note/delete/95681/non-english
Del: in docs -- https://master.php.net/note/delete/95681/in+docs
Del: other reasons-- https://master.php.net/note/delete/95681
Reject -- https://master.php.net/note/reject/95681
Search -- https://master.php.net/manage/user-notes.php