|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-08-26 02:57 UTC] kexianbin at diyism dot com
Description:
------------
Currently, php 5.3.0 doesn't support automatic type casting for the variable function name, we indeed need it, it could lead to very cool applications.
Reproduce code:
---------------
<?
class cls_so_work
{function __toString()
{return 'so_work';
}
}
function so_work()
{echo 'hello';
}
$o=new cls_so_work;
echo $o.'<br>';
$o();
?>
Expected result:
----------------
so_work
hello
Actual result:
--------------
so_work
Fatal error: Function name must be a string in E:\WWW\PhpApps\test\test.php on line 18
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 22:00:01 2025 UTC |
In fact, i make the above request for the "SoWork PHP Framework": <? define('this', mt_rand()); class cls_so_work {function __call($fun, $pars) {foreach ($pars as &$v) {if ($v===this) {$v=$this->val; break; } } $tmp=eval('return defined("'.$fun.'")?constant("'.$fun.'"):"'.$fun.'";'); $this->val=call_user_func_array($tmp, $pars); return $this; } function __toString() {return 'so_work'; } function cls_so_work() {$obj=func_get_args(); $this->val=isset($obj[0])?$obj[0]:null; } } function so_work() {$obj=func_get_args(); if (isset($obj[0])) {return new cls_so_work($obj[0]); } else {if (!isset($GLOABALS['so_work'])) {$GLOABALS['so_work']=new cls_so_work(); } else {$GLOABALS['so_work']->val=null; } return $GLOABALS['so_work']; } } $o=so_work(); define('echo', 'my_echo'); function my_echo($obj) {echo $obj; return $obj; } //$o('abcd')->substr(this, 2, 2)->strlen(this)->echo(this); $o->substr('abcd', 1, 3)->strlen(this)->echo(this); ?> The commented line in the end will raise the error.