What is a language constructor?

9

In PHP, I've read and heard many times about Language Builders. The cases I've heard of them have been in cases where it was said "you'd rather use X instead of Y, since Y is a function and X is a language constructor."

One of the cases I've read about would be if I use isset to check an index of array , instead of array_keys_exists I know the difference between the two, but not is the focus of the question ). The recommendation is because array_key_exists is a function, and isset , a constructor.

So, I wanted to know a few things:

  • What would a language constructor be in PHP? What are they?

  • In cases where language constructors resemble functions (such as isset , empty and require ), what are the main differences between language constructors and functions? >

An example of question 2: Use isset instead of is_null (which is a function), where isset also returns True if the value of the variable is NULL .     

asked by anonymous 30.08.2018 / 14:35

2 answers

9
  

What would a language constructor be in PHP? What are they?

Some will not like this, but I have to say that it is usually gambiarra, at least most cases in PHP.

Everything that the compiler deals with in a special way can be considered as language construction (I do not like the term constructor there because it is not quite an agent but a feature, so I'll use construction). A function in general is a construction of language, because it deals in a special way. I mean, role-building itself is a construction. if is a construction. But using a function is something of the user, even if it is a standard function the library no longer belongs to the language, the function has no special meaning.

  

Why does the PHP documentation recommend in some cases to use language constructors instead of functions?

I think because it is more efficient, or because it is already ready, or because it is the only option (depends on the case). In fact a construct can be more efficient because the compiler / interpreter can handle it better than a function could, but in some languages this does not occur or is not necessary, or the gain is negligible. Curiously, script languages, as is the case with PHP, do not need a lot of optimizations and a function would be appropriate. Language constructs can be useful when the compiler can guarantee better target code. And interestingly these languages have the ability to optimize functions to such an extent that it makes building unnecessary. So we can talk about PHP compiler crash too.

Although today I think there is a lot of it to bequeath. Maybe today would make it different. Or not, after all in every version of this language they put new bad things.

  

In cases where language constructors resemble functions (such as isset, empty, and require), what are the main differences between language constructors and functions?

The require I think it should be construction even because it changes the source code being worked there, it has a greater influence on execution, but in PHP it changes less. Note that it can be used up without parentheses, as can echo and so others can be used without.

Who knows, the fact that you did not force the parenthesis was the reason to prefer to be a construction of language. It has language that allows you to leave them aside in some cases of functions without having to be language constructs.

For example,% w / w% can probably have some optimization because it deals with various types and there would be a huge% w / o of% to select the action according to the argument used and this would have a cost, even if not always expressive because most types would be at the beginning of the table and and the checks are not or should not be done one by one but will know if empty() of PHP is not optimized like this.

The switch may be because it involves things inside the language, but could be rather a function.

    
30.08.2018 / 14:52
1
  

What would a language constructor be in PHP? What are they?

The language constructor or reserved words are the grammars defined / used by the lexical parser to interpret a syntax. Programs such as lex / flex and yacc / bison that compose this universe of lexical analysis. For example: If you want to write a new language you will have to define which commands it accepts or not, in which order it should be written and define every detail so that the computer can interpret the commands and even know what to do when something is written in their new language.

Let's say you have defined the words ("look up" and "negatively"), these words will be reserved. the lex / flex together yacc / bison work to read these entries and generate an already optimized C code to recognize their syntax and treat it as you have defined it. The "isset" command for php is also defined in the language grammar, more specifically in the file zend_language_scanner.l source code is available if you want more information. The function "array_key_exists" is not present in the lexical constructor. It is not a reserved word for not doing the php (language) syntax, but it does exist and adds functionality to the php core and makes up the standard php module, "array_key_exists" you can find more information in " php_array.h " and " array.c ", as can be seen unlike the isset that is defaults to constructing the syntax, it is not defined in the language creation, but rather "added" "PHP_FUNCTION (array_key_exists)", just as you might could be creating a module itself and adding any other function .

You can find some token used in direct in the manual or at source code . If you wish, you can also find in the stackoverflow a post about analysis lexical also has a lot of interesting stuff about it on the internet.

Note:

lex, flex, yacc and bison are different programs. And inserted in the context just to facilitate, if you want more references on the subject

  

Why does the PHP documentation recommend in some cases to use language constructors instead of functions?

In the case of isset and array_key_exists the recommendation I found on the site is due to performance issues, as in the php documentation page itself, one of the suggestions is not to replace, but to use both. There is a Benchmark, showing the runtime of the algorithms.

The reason for one being faster than the other is not exclusively that one is a language constructor and another is a function, but also the complexity of the algorithms to execute an isset or an array_key_exists.

The complexity of the algorithm can cause you to gain or lose performance depending on the volume of use. As each case is a case, the ideal is to read both the documentation and the note for more information, and if there is still doubt, need and / or interest is possible to access the source code.

In isset and array_key_exists what was pointed out was the performance, but may be issues since depreciation, retro compatibility and etc ..., but I say again, each case is a case.

  

As a matter of curiosity this is the implementation of array_key_exists in   php 7.1

PHP_FUNCTION(array_key_exists)
{
    zval *key;                  /* key to check for */
    HashTable *array;           /* array to check in */

    ZEND_PARSE_PARAMETERS_START(2, 2)
        Z_PARAM_ZVAL(key)
        Z_PARAM_ARRAY_OR_OBJECT_HT(array)
    ZEND_PARSE_PARAMETERS_END();

    switch (Z_TYPE_P(key)) {
        case IS_STRING:
            if (zend_symtable_exists_ind(array, Z_STR_P(key))) {
                RETURN_TRUE;
            }
            RETURN_FALSE;
        case IS_LONG:
            if (zend_hash_index_exists(array, Z_LVAL_P(key))) {
                RETURN_TRUE;
            }
            RETURN_FALSE;
        case IS_NULL:
            if (zend_hash_exists_ind(array, ZSTR_EMPTY_ALLOC())) {
                RETURN_TRUE;
            }
            RETURN_FALSE;

        default:
            php_error_docref(NULL, E_WARNING, "The first argument should be either a string or an integer");
            RETURN_FALSE;
    }
}
  

In cases where language constructors resemble functions (such as isset, empty, and require), what are the main differences between language constructors and functions?

The "Language builders" as quoted above would be the grammar of the language "consult lexical analyzer". The functions "in this case" would be other functions (functions) defined in modules and extensions of php.

I hope this helps you in some way.

    
05.09.2018 / 22:10