|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-07-09 13:28 UTC] thijs dot wijnmaalen at gmail dot com
Description:
------------
When accessing a method within or from another class, whereby the name
is stored as value in an array as attribute in the class.
The proper way of doing this would be to enclose the method name in
brackets (which works as expected):
return $this->{$this->_[0]}();
Reproduce code:
---------------
<?php
class A {
private $_ = array();
function __toString () {
$this->_[0] = 'a';
return $this->$this->_[0]();
}
function a () {
return 'call';
}
}
echo $a = new A();
?>
Expected result:
----------------
Syntax error message
Actual result:
--------------
Empty reply from server
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 22 11:00:01 2025 UTC |
> return $this->$this->_[0](); Here PHP will try to read the property $this->$this. So it will try to convert $this to a string, which will call __toString(), etc and it crashes. This is basically the same as the following code: <?php class A { function __toString() { return (string)$this; } } echo $a = new A; ?>