Declaring a class with a reserved word name is a good idea?

11

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 .

    
asked by anonymous 02.03.2015 / 13:49

2 answers

5

In reality you can not use reserved words (so-called language constructs) anywhere. The functions that you were able to create and work ( Int , String , Object ) are not reserved words, as per the documentation .

You do not have to worry about declaring methods and functions against existing keywords. You can put them in the variables, but it can cause some confusion.

The interesting thing is to avoid expressions that may be possible reserved words in the future, such as int , string , float . With the proposed type hinting of primitive types in PHP 7 and a new operator in the language , new reserved words may arise, as already proposed here > and here .

The problems that may occur are incompatibility with legacy codes, as you exemplified with the word yield in Laravel 3 (I can not remember if a patch was created to fix this).

To mitigate this type of situation, a proposal that allows the use of some reserved words in method scope . This would allow the developer more freedom in naming their methods and would avoid this type of mismatch described above.

With this proposal approved, we could develop more expressive codes like this below:

class Collection extends \ArrayAccess, \Countable, \IteratorAggregate {

    public function forEach(callable $callback) {
        //...
    }

    public function list() {
        //...
    }

    public static function new(array $itens) {
        return new self($itens);
    }
}

Collection::new(['foo', 'bar'])->forEach(function($index, $item){
  /* callback */
})->list();
    
02.03.2015 / 15:51
3

Let's break it down:

  

I should worry about not declaring classes and functions with the name of these   keywords described above (whether accepted in the declaration, or   no)?

Yes. You should avoid using any reserved word in the declaration of your variables / classes / methods.

  

Or it's okay to do this, since the famous frameworks   above use this practice?

There are problems with doing this. A myriad of anomalies can occur, from updating PHP (in your case) to later maintenance.

  

Is there any recommendation when to which names can be used or   not used when it comes to a keyword name? Because   when will I know that I am "liberated" to declare a   class with that specific name.

In theory, the language manual should state whether any reserved words can be used, but I highly doubt that PHP has this. First of all because, in theory, reserved words are ... well, reserved for the correct interpretation / compilation of the code.

I believe you are encountering these confusions because of PHP, which tries to start from a "run as often as possible" assumption. Most likely other languages would not allow reserved words in any case, so it does not make much sense to try to find the reserved words that can be used as class / method names.

    
02.03.2015 / 14:07