php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #49367 Automatic type casting for the variable function name
Submitted: 2009-08-26 02:57 UTC Modified: 2009-11-02 11:36 UTC
Votes:2
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: kexianbin at diyism dot com Assigned:
Status: Wont fix Package: Feature/Change Request
PHP Version: 5.3.1 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: kexianbin at diyism dot com
New email:
PHP Version: OS:

 

 [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 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-08-26 03:02 UTC] kexianbin at diyism dot com
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.
 [2009-11-02 08:57 UTC] kexianbin at diyism dot com
Change expected version to 5.3.1 from 5.3.0
 [2009-11-02 11:36 UTC] colder@php.net
If you want to handle what goes on when $o(); is used, see the 
__invoke() magic method 
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 27 12:01:29 2024 UTC