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.
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.
– Sileno Brito
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.
– Sileno Brito
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.
– Sileno Brito
@Silenobrite Wrong.
isset
checks if the variable exists and if it is notNULL
. 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.– Wallace Maxters
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".
– Wallace Maxters