Generally, when we are going to define names for class and functions, there is a concern when colliding with palavras-chaves
of language.
The curious thing is that I noticed that in PHP it is allowed to define class and functions with the same name of certain keywords.
Settings that do not generate errors
For the following examples below, the setting DOES NOT generate errors.
class Int{}
class Object{}
class String{}
Even in Cakephp
there is a class called Object
. And, in ZendFramework
, there is Zend\Form\Annotation\Object
.
And we still have the functions. Note that it seems to contradict the statement below:
function int($int)
{
return (int) $int;
}
var_dump(int('1')); // int(1)
Settings that generate error
In the examples below, I used two keywords. A very common one, which is array
, and another is the one that was added in the 5.4
version of PHP, which is callable
.
class Callable{}
class Array{}
And we have the following result:
syntax error, unexpected 'Callable' (T_CALLABLE), expecting identifier (T_STRING)
syntax error, unexpected 'Array' (T_ARRAY), expecting identifier (T_STRING)
The questions
-
Or is it okay to do this, since the above-mentioned famous frameworks use this practice?
-
Is there any recommendation when to which names can be used or not used, when it comes to a name of a
palavra-chave
? For when will I know that I "am released" to declare a class with such a specific name.
Note
I just wanted to register a problem that occurred for those who used framework versions Laravel 3
. In it there was a function called yield
. Laravel 3
was developed for PHP 5.3
. But later, PHP 5.5
"invented" the reserved word yield
, which made Laravel 3
not work in PHP 5.5, as it generated E_PARSE
. And so the developer would have to be stuck between version 5.3 and 5.4 of PHP, if he kept Laravel 3
.