3

While searching for something else, I found this code

$x = "foo";
    function foo(){ echo "wtf"; }
$x(); # "wtf"   

and when I searched google for this, I got only C results not PHP, while I know that under the PHP hood is C, I would really like some explanation about that.

What is that called?

and how it is interpreted by PHP zend? is it some sort of callable?

and is it still available in PHP 7+ ?

3 Answers 3

5

This is called variable functions in PHP : http://php.net/manual/en/functions.variable-functions.php

Basically, when there is a variable before the (), first the variable get resolved and interpreter get the value of it and try to find the function with that value. If the function exists, it calls or function not found exception will be thrown.

Sign up to request clarification or add additional context in comments.

2 Comments

This answer is a little light on details. Could you perhaps expand on your answer? Currently it looks more like a comment.
Thanks for your answer, however that didnt include Q2 thoroughly and Q3.
2
  1. It is a callable (variable function: http://php.net/manual/en/functions.variable-functions.php )

  2. Zend just treats it as a callable and calls the function.

  3. It is an old and stable language feature, and still available in PHP7+ (and it will be available as long as possible)

The callable types and normalizations are given in the table below: http://php.net/manual/en/language.types.callable.php#118032

Callable                        | Normalization                   | Type
--------------------------------+---------------------------------+--------------
function (...) use (...) {...}  | function (...) use (...) {...}  | 'closure'
$object                         | $object                         | 'invocable'
"function"                      | "function"                      | 'function'
"class::method"                 | ["class", "method"]             | 'static'
["class", "parent::method"]     | ["parent of class", "method"]   | 'static'
["class", "self::method"]       | ["class", "method"]             | 'static'
["class", "method"]             | ["class", "method"]             | 'static'
[$object, "parent::method"]     | [$object, "parent::method"]     | 'object'
[$object, "self::method"]       | [$object, "method"]             | 'object'
[$object, "method"]             | [$object, "method"]             | 'object'

1 Comment

Thank you .. this is the line that I'm seeking to understand A PHP function is passed by its name as a string. under Passing section.
0

A variable function. The variable x is simply holding a value which is equal to function name. Calling variable that way results to calling the function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.