How to know the difference between language construction and function in PHP?

Asked

Viewed 190 times

2

How to identify and what is the difference between a language construction and a function?

2 answers

7


Checking with function_exists:

PHP has a suitable function for this, which returns true for functions, but returns false for constructions like require, etc..:

bool function_exists ( string $function_name )

See working on IDEONE.

Handbook: function_exists.


Listing with get_defined_functions:

This function already brings the list of all functions defined at once

array get_defined_functions ([ bool $exclude_disabled = FALSE ] )

And to complement, this list the constants defined and their values:

array get_defined_constants ([ mixed $categorize ] )

See both working on IDEONE.

Handbook: get_defined_functions and get_defined_constants.


Now, if you’re going to check day-to-day when programming, just reading the manual, that has a list of Keywords or looking at the source, since in PHP things were kind of "implemented with the eye". It would not compensate you to make a font to test each one (unless you generate a small tool with a form, to refer to the function_exists at any time)

7

Question already answered by Bacco, I will put something more theoretical.

PHP is a somewhat irregular language, created by who admits to having done this without knowing. Apparently there was no understanding of what each thing is and commands or other forms were created in the language that actually should be functions.

Of course, eventually it might be useful to have a construction to avoid, for example, not using parentheses, but it would be possible to standardize this. It is usually not necessary and the adoption of a construction is more by not having the creativity to have a more universal solution. It can also be other reasons that it acts in a special way or even for no reason, who created it thought it best to do so for the sake of it, or forgot how it was done before, and then it becomes inconsistent.

Why the echo needs to be a construct? Just because it may not use parentheses.

In some cases it may be because of performance, but today it makes less sense. A construction has gained because it is written in C, is optimized to avoid some unwanted and unnecessary cost and because it does not have the cost of mapping, but optimizations can account for this.

Another reason is that some checks can be done before running when you have a construction, the function, especially in PHP, tends to generate error only in execution.

The construction differs by forcing the compiler to understand it and know what to do, it is a special Lego piece.

Peças Lego (blocos de construção) diversos formatos

While the function is something standardized and the compiler (or interpreter, whatever) just needs to know that it’s a function and it needs to exist with that name, it’s a normal Lego piece.

Peça Lego (blocos de construção) padrão

When building the compiler needs to be developed beforehand to understand each part of what will be allowed there, both in syntax and semantics. The function is just something that will be delegated to another part of the code eventually passing arguments in a very regular way and maybe return a result.

The function does not even need to be language, it makes no difference whether it is internal to PHP, or yours, or a library. Of course, inmates can perform better because it may actually be a C function exposed to PHP, but you don’t even need to know that. Even C functions cost a little more than if it were pure C because it needs a mapping.

When it has a function it is equal to a variable, in fact the variable name and the function name are called identifiers, there is no difference. It is that the function name is like a variable of a special type that allows you to call and execute a code. Newer versions of PHP allows it to be even passed between normal variables, which is called an anonymous function and can be expressed through a lambda and even contain a mechanism of closure.

A language construct can’t do that. Not even an operator that is much like a function and internally can even be a function, cannot be used this way.

One thing few know is that it is possible to create functions in C and expose for your PHP code, only it is not the easiest language to do this.

Of course there are cases that it is more complicated something be treated as a function, even it can pass the wrong idea, or have inefficiencies, why no language mainstream has the if for example, as a function.

One thing I always say is that the programmer only learns to program even when he understands everything that is occurring in his code, which he knows even when to use a blank space or not, even when it is optional. An example is this, you can use:

if(condicao)

But this seems a function, already using:

if (condicao)

It becomes clearer that it is a language construct that has an expression surrounded by parentheses, since they are also used as an indicator of arguments in functions, even if it is zero, or to group sub-expressions. Being ambiguous better write in a way that makes clear what the intention is.

Often when it looks like a function, but it’s a language construct, we call it built-in Function. And it can be manipulated internally through function override_function() or rename-function() and change the behavior or name. But only use if you know a lot what you’re doing.

So as Bacco said, just by consulting the manual to learn what each thing is, and eventually you can check at runtime if it’s a function, but don’t do it in normal code in production, This is an advanced way to mount executions that you don’t know what it is until the code runs. If you are going to do it make sure you are already very experienced, because a lot of things can go wrong if you start trying to execute code that has no control during development.

Browser other questions tagged

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