|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2004-12-02 12:43 UTC] joel at zmail dot pt
Description:
------------
__call doesn't return by reference
Reproduce code:
---------------
class A{
private $x = 123;
public function & __call($m,$a){
return $this->x;
}
}
$a = new A();
$x = & $a->UndefMethod();
$x = 789;
$y = & $a->UndefMethod();
Expected result:
----------------
$y should be 789, but it is 123
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 16 09:00:01 2025 UTC |
With all due respect, "only variables should be assigned by reference" makes no sense. __call does not assign variables. It executes code for innaccessible methods, and that code may wish to return a reference. Not bogus; this functionality existed in PHP4 as __call()'s third parameter was passed by reference. Although this is in no way justification for the fix (as I have already justified it) consider this: (pedantic error checking removed for simplicity) <? class Foo { private $world; function &__call($method,$params) { if( substr(0,3,$method) == 'get' ) return $this->{substr($method,3)}; } function &__get($property) { return $this->{'get'.$property}(); } } $hello = new Hello; $hello->world[] = 'php maintainers always say NO'; ?> --which does not work. For those of us mere 'users' who need this, as a work around try this: <? class Hello { . . . include definition from above . . . function &getWorld() { return $this->world; } } ?> --which DOES work, although that is not really the point!