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

Question:

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

echo gettype($f);

Returns Object when theoretically looking at it was to return function .

In JS :

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

Returns function .

Answer:

Introduction

It is at the discretion of each language to define how to organize its entire structure, that is, each one does what it thinks works best for it. It would be possible for all data to have only one type, it could be an object or another. But this is bad for organizing more complex codes, that's why almost all languages, and all mainstream ones, have 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 information?

Typing

According to type theory dynamic typing languages ​​like PHP and JS only have one type, but they have internal type tags and we informally deal with them as if they were different types, just make sure this is formally wrong. I will speak the way everyone understands.

So some types have more specific tags , including every value that has the Object tag has another more internal tag that says the type of this Object , but for typing purposes only the Object level is shown.

For some reason they wanted to separate what an 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 the complexity they have today. Wouldn't it be easier for everything to be Object and just have its internal tags ? That way you could just deal with the tags and forget about the types, because in the end everything is the same.

Well, they didn't do that and decided that some data types would be on the same level as Object . This is not unique to these two languages, in general all dynamically typed languages ​​are like this. Static typing languages ​​put everything on the same level because they don't use type tags , they are really types. Unfortunately they err in not having a single common type, as C# does, this one got it right. Even Java made this mistake.

Motivation

JavaScript felt that a function was so important to it that it deserved to have an isolated type, just as it has String, Number , Array`, etc. Deep down they are all in some way an object.

PHP didn't find this so important. As this mechanism was created later, it may even have a compatibility issue, but I'm just speculating, I believe it doesn't even have this type of problem. And that's what I say, PHP was not created and is not evolved by people who understand a lot of computational concepts. People don't like to hear this, but it's a fact even stated by the creator. So they created a new syntax for the anonymous function, so they consider it important, but not enough to type it in a special way. Doesn't it look unbalanced?

But there is nothing that requires returning a more specific type.

how to get what you want

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

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

See working on ideone . And on repl.it. Also posted on GitHub for future reference .

Note that it returns Closure because it's not just a function, it's a closure, just as it is in JS. Each name is what you want, conceptually PHP is more right, because in both this is a closure and not just a function.

Scroll to Top