What is a language builder?

Asked

Viewed 271 times

11

In PHP, I have read and heard several times about Language Builders. The cases I’ve always heard of were in cases where it was said: "prefer to use X rather than Y, because Y is a function and X is a language constructor".

One of the cases I’ve read about would be in case I use isset to check an index of a array, instead of array_keys_exists (I know the difference between the two, but it’s not the focus of the question). The recommendation is because array_key_exists is a function, and isset, a builder.

So I wanted to know a few things:

  • What would be a PHP language builder? What are they?

  • Why the PHP documentation recommends, in some cases, using language constructs instead of functions?

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

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

  • isset does not check if it is null only but if the variable itself exists, when you create a variable it occupies address in memory, but the value of its contents can be null. In this case if you use isset it will return true, not because it is not null, but because it exists somewhere in memory, is_null assumes that the variable exists and tests its value, not if it is allocated somewhere. If the variable is not even instantiated isset will return false, not because it is null, but because it does not occupy address in memory.

  • php constructor is a method used to define the default parameters of your object, when you create a class in any language you are actually asking the operating system or memory manager to reserve a specific memory space (necessary to allocate the attributes and other resources of your object), so the constructor allows you to define the default values for your object and returns the address it allocated to that object.

  • PHP, like other languages, recommends using constructors not because you should always do that, but because many of the times you might be using a resource wrong, because you don’t know exactly what it’s for.

  • @Silenobrite Wrong. isset checks if the variable exists and if it is not NULL. See on documentation. A lot of people who use PHP think exactly what you said, but are wrong. The documentation makes it very clear: Determine if a variable is set and is not NULL. Of course, the documentation in Portuguese doesn’t help either.

  • 1

    About Builder: Wrong again. I’m not talking about a class builder, I’m talking about "language builder" (Languange Construct), which are the famous "reserved words".

2 answers

10


What would be a PHP language builder? What are they?

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

Everything that the compiler treats in a special way we can consider as language construction (I don’t like the term constructor there because it’s not really an agent but rather a feature, so I’m going to use construction). A function in general is a construction of language, because it treats in a special way. I mean, the creation of function itself is a construction. The if is a building. But using a function is something of the user, even if it is a standard function of the library no longer belongs to the language, the function has no special meaning.

Why the PHP documentation recommends, in some cases, using language constructs instead of functions?

I believe that because it has more efficiency, either because it is already ready, or because it is the only option (it 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 certain languages this does not occur or is not necessary, or the gain is negligible. Interestingly languages of script, As is the case with PHP, they don’t need much optimization and a function would be appropriate. Language constructs can be useful when the compiler can guarantee a code target better. And interestingly these languages have the ability to optimize functions to such an extent that it makes the construction unnecessary. So we can talk about PHP compiler failure as well.

Although today I think there’s a lot of that going around. Maybe today you’d do it differently. Or not, after all in every version of this language put new bad things.

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

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

Who knows the fact that not forcing the parentheses has been the reason to prefer language construction. Has language that allows you to set them aside in some cases of functions without needing to be language construction.

The empty() for example probably can have some optimization because it treats of several types and then would have a huge switch 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 the switch PHP is not optimized like this.

The isset() may be because it involves internal things of language, but it could be rather a function.

  • Addendum: When is a language construct, it is not possible to store the name in a variable to use through call_user_func or $f = 'isset'; $f($var).

  • Certainly.....

  • I’m sorry, but I can’t see how the constructor of a language can be considered a gambit. Also I could not see that the goal of a constructor would be to obtain more efficiency or that it is not necessary to have optimization in scripting languages. His answer was almost entirely based on "I don’t know" and "I think".

  • 3

    @Silenobrito posts his answer saying what it is. I know it is this, within the knowledge I have in more than 30 years studying programming languages. There is no official information about this, but it is public knowledge that these constructs are unnecessary and may only be functions done in the right way, and have several web-based posts about these and other PHP scams. It is also universal knowledge that scripts do not need performance. If you have a clear reason for choosing to do so should be documented. There is probably no reason why.

  • 4

    And from what I understand you’re talking about something completely different than what the question is about. If you start talking about the same thing, it might also start to make sense. No one is talking about a class builder here. Reread the question and the answer to understand what it is. After that it makes more sense. If you still do not show in your comments or answer why you disagree with these things, if you still disagree. We are talking here about decisions of design language, and even very specific in what mechanism, and not on class mechanisms.

  • @Maniero was right, we were talking about different things. The comments I made in the post do not really apply, because I really confused with the title and the post, I thank you for alerting me. However, even though I have been wrong before, after rereading, I still do not agree with his statements. The reasons are the same as I commented earlier.

  • 1

    @Silenobrito and I disagree with her answer since she says that the construction of language gives more performance because it has more interesting complexity, but any function can have the same complexity, if the reason was that it would not make sense to be a construction of language, which is good because your answer reinforced mine which says it is something unnecessary and was only done because the design of language is a gambiarra. Although it is true about complexity (I guess because I didn’t go deep), it is irrelevant to the question that is about the design of language. Even disagreeing I did not deny it.

  • @Maniero in the summary of all this, although we disagree with each other the good thing is that it will have at least two different points of view to add value to the question. I’m going to reread, because I don’t remember writing that the construction of the most performance, it serves to validate the syntax. Performance is conditioned by the complexity of the algorithm.

Show 3 more comments

2

What would be a PHP language builder? What are they?

The language constructor or reserved Palavas 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 what order should be written and define each detail so that the computer can interpret the commands and even know what to do when something is written in its new language.

Suppose you have defined among others the words ("search" and "negative"), these words will be reserved. lex/flex together Yacc/Bison work to read these entries and generate C code already optimized to recognize your syntax and treat it as you set it. The php "isset" command is also set in language grammar, more specifically in the file zend_language_scanner. l the source code is available if you want more information. The "array_key_exists" function is not present from the lexical constructor. It is not a reserved word because it does not make the syntax (language) php, but it exists and adds functionality to the core of php and composes the standard php module , "array_key_exists" you can find more information on "php_array. h" and "array. c", as can be observed unlike the isset that is defined in the syntax construction, it is not defined in the language creation, but "added" "PHP_FUNCTION(array_key_exists)", just as you could be creating your own module and adding any other function.

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

Observing:

lex, flex, Yacc and Bison are different programs. And inserted in context only to facilitate, if you want more references on the subject

Why the PHP documentation recommends, in some cases, using language constructs 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 running time of the algorithms.

The reason one is faster than the other is not exclusively the fact 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 to get more information, and if there is still doubt, need and/ or interest is possible to have access to the source code.

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

As a curiosity, this is the implementation of the 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 constructs resemble functions (such as isset, Empty, and require), what are the main differences between language constructors and functions?

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

I hope it helps you somehow.

Browser other questions tagged

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