php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #46120 Suggestion for expansion of function variable syntax
Submitted: 2008-09-19 03:09 UTC Modified: 2008-09-19 06:55 UTC
From: mephtu at yahoo dot com Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.2.6 OS: Ubuntu Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: mephtu at yahoo dot com
New email:
PHP Version: OS:

 

 [2008-09-19 03:09 UTC] mephtu at yahoo dot com
Description:
------------
I want to be able to execute ad hoc function variables.



Reproduce code:
---------------
<?php
function do_bucket() {
  echo 'Hello, world!';
}

$tag = "bucket";
$'do_'.tag();
?>

Expected result:
----------------
Hello, world!

Actual result:
--------------
Error.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-09-19 06:55 UTC] tularis@php.net
That has never worked, not even for variables. All of the following will work fine:
$t = 'do_'.$tag;
$t();

Your problem is that your syntax is basically impossible. It can not distinguish between:
$'do_' . tag();
which might be either something remotely looking like a variable (parse error'd of course) followed by a concatenation operator, followed by a function call to function tag(). OR
which might be that you want to concatenate everything together, and THEN execute the function by that name.

The thing is, the parser can't differentiate between those two, so it doesn't even try. You can already execute "ad hoc function variables", as long as you use proper syntax.

It is unfortunate that something like:
${'do_'.$tag}();
does not work, but instead returns 
Fatal error: Function name must be a string

Anyway, marking as bogus since you already can do this.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Jun 02 00:01:30 2024 UTC