Is declaring a class named after a reserved word a good idea?

Asked

Viewed 437 times

11

Generally, when we define names for class and functions, there is a concern when colliding with the 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 as certain keywords.

Definitions that do not cause errors

For the following examples below, the definition does NOT generate errors.

class Int{}

class Object{}

class String{}

Including in the Cakephp there is a class called Object. And, in the ZendFramework, exists Zend\Form\Annotation\Object.

And we still have the functions. Note that it comes to seem contradictory to the statement below:

function int($int)
{
    return (int) $int;
}

var_dump(int('1')); // int(1)

Definitions 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 version 5.4 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

  • I must worry about not declare classes and functions with the name of these keywords described above (whether accepted in the declaration or not)?

  • Or there is no problem in doing so, since the famous frameworks above use this practice?

  • Is there any recommendation when names can be used or not used, when it is 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 record a problem that occurred for those using framework versions Laravel 3. In it there was a function called yield. The Laravel 3 was developed for the PHP 5.3. But later, the PHP 5.5 "invented" the reserved word yield, that caused the Laravel 3 did not work in PHP 5.5, as it generated a E_PARSE. And so developer would have to get stuck between version 5.3 and 5.4 of PHP if they kept the Laravel 3.

  • 1

    What if language allows some reserved words to be used in methods and leaves you more comfortable to develop your classes the way you want? https://wiki.php.net/rfc/context_sensitive_lexer

2 answers

5


In reality you cannot use the reserved words (so-called language builders) anywhere. The functions you were able to create and worked (Int, String, Object) are not reserved words, according to the documentation.

You do not need to worry about declaring methods and functions in relation to already existing keywords. You can put them in 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 proposal of type hinting of primitive types in PHP 7 and a new operator in language, new reserved words may arise, as already proposed here and here.

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

In order to alleviate this type of situation, a proposal is being voted on to allow the use of some reserved words in the scope of methods. This would allow the developer more freedom in naming his methods and avoid this type of incompatibility described above.

With this approved proposal, 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();

3

Let’s go in pieces:

I have to worry about not declaring classes and functions with the name of these key words described above (either accepted in the declaration, or nay)?

Yes. You should avoid using any reserved word in declaring your variables/classes/methods.

Or there is no problem in doing this, since the famous frameworks use this practice?

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

Is there any recommendation when names can be used or unused, when it comes to a keyword name? Yeah when will I know that I "am released" to declare a class with such a specific name.

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

I believe you are finding these confusions because of PHP, which tries to start from an assumption of "run whenever possible". Most likely other languages would not allow reserved words in any case, so it doesn’t make much sense to keep trying to find reserved words that can be used as class names/methods.

  • I only disagree with the variables in the specific case of PHP, because it always has $ before.

Browser other questions tagged

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