2

Is there a built-in class function which instances are the standard PHP functions? Are PHP functions objects?

In Python I can test it in this way:

from inspect import isclass
def foo():
    pass
isclass(type(foo))
>>> True

What about this function in PHP:

function foo(){
    return null;
}
1
  • User defined functions such as your foo example are not a class in PHP, but a language construct. Pretty much like if or interface. As Carlos pointed out, PHP automatically transforms a function assigned to a variable into an instance of the class Closure. Commented Feb 26, 2016 at 11:04

1 Answer 1

6

Anonymous functions are objects of "Closure" class. Here is the test:

$myfunction = function(){
    echo "Hi";
};

if(is_object($myfunction)){
    echo get_class($myfunction); //prints 'Closure'
}
Sign up to request clarification or add additional context in comments.

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.