Why is an anonymous function seen as an object in PHP?

Asked

Viewed 229 times

8

$f = function() {
    echo "OK";
};

echo gettype($f);

Returns Object when theoretically looking was to return function.

-

In JS:

var f = function() {};   
typeof f;

Returns function.

  • 1

    You have an object that holds a function. If you make $f();, it will work.

  • In fact, gettype has no return from function. See: http://php.net/manual/en/function.gettype.php

  • In fact an anonymous function in PHP is Closure: Class used to represent anonymous functions. That is, whether it is a class is of the type object.

3 answers

10


Introducing

It is discretionary of each language to define how to organize its entire structure, that is, each one does as it thinks works best for it. It would be possible for all data to have only one type, could be one object or other. But this gets bad to organize more complex codes, so almost all languages, and all mainstream has other types, even to know more easily what to do. Can you imagine how difficult it would be to perform operations when you don’t have any type of information?

Typing

According to the type theory dynamic typing languages, such as PHP and JS, only have one type, but they have tags of internal types and informally we deal with them as if they were different types, just be sure that this is formally wrong. I’ll talk the way everybody understands.

So some guys have tags more specific, including any value that has the tag Object there’s another tag more internal that says the type of this Object, but for the purpose of typing only the level of Object is shown.

For some reason they wanted to separate what is a Object and what other types are. I consider this a mistake. But it makes sense because these languages were created without thinking that they would have this complexity that they have today. It wouldn’t be easier to be Object and only have the tags internal? So you could handle only the tags and forget the guys, because deep down everything is the same.

Well, they did not do so and decided that some types of data were on the same level as Object. This is not unique to these two languages, in general all dynamic typing languages are like this. Static typing languages put everything on the same level because they don’t use tags of types, and yes they are types. Unfortunately they are wrong in not having a single common type, as C# has, this right. Even Java made this mistake.

Motivation

Javascript considered that a function was so important to her that it deserved to have an isolated type, just as it has String, Number, Array`, etc. At the bottom they are all somehow an object.

PHP didn’t think it was that important. Since this mechanism was created later on, it may even have some compatibility issue, but I’m just speculating, I don’t even think you have that kind of problem. And that’s what I’m talking about, PHP wasn’t created and it’s not evolved by people who really understand computational concepts. People do not like to hear this, but it is a fact even stated by the creator. So they created a new syntax for anonymous function, so they consider it important, but not enough to typify this in a special way. It does not seem unbalanced?

But there’s nothing to return a more specific type.

How to get the want

If you really want to know tag of the kind Object you have to use get_class():

$f = function() {
    echo "OK";
};
echo get_class($f);

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Note that returns Closure because it’s not just a function, it’s a cloister, just as it is in JS. Each one gives the name you want, conceptually PHP is more right, because in both this is an enclosure and not just a function.

8

How PHP handles anonymous functions

In PHP, an anonymous function is an object of the type Closure. Language documentation says the following about the Closure class:

Class used to represent Anonymous functions.

In your example, the function in question stored in the variable $f is an object because it is an instance of Closure, so the type test result says that the value in question is an object and not a function (as in the case of Javascript).

1

Because there is no data type "Function". The function gettype() php returns the type of the variable. The return data types of the gettype function are:

  • "Boolean"
  • "integer"
  • "double" (for historical reasons "double" is is is returned in the case of float, and not simply "float")
  • "string"
  • "array"
  • "Object"
  • "Resource"
  • "NULL"
  • "Unknown type"

Its function is like a anonymous function, a closure. A closure has return of objects of this type. If you give a var_dump in your variable f was will return: object(Closure)#1 (0) { } . You can also validate that it is an object like this var_dump(is_object($f)); it will return TRUE as it is an object. Also this way stating that it is a closure echo get_class($f);.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.