In PHP 7, can the class method contain a keyword name? this is good or bad?

5

PHP 7, has released a number of innovations for the PHP language. Also, I noticed that classes can now have methods with keyword names. The strange thing is that classes can not have, but methods can (vixe!)

For example, this is valid in PHP7:

class X{
 public function foreach(array $array, \Closure $closure) : boolean {
 }
}

My curiosity is: Is this good or bad?

Is it really a benefit to be able to declare the methods name as being of a keyword?

What was the reason why PHP-7 included this new functionality in the language, if it previously generated a Parse Error ?

    
asked by anonymous 16.05.2016 / 16:38

1 answer

5
  

Globally reserved words as property, constant, and method names within classes, interfaces, and traits are now allowed. This reduces the surface of BC breaks when new keywords are introduced and avoids naming restrictions on APIs .

     

This is particularly useful when creating internal DSLs with fluent interfaces .

There is a reason for this. According to the manual this reduces compatibility breakdown if a new keyword is added and has been used as a 'function' such as yield in Laravel ( example of SOen problem ). The other advantage is to maintain the vocabulary in creating a DSL (domanin specific language), ie you do not have to search for a synonym.

Example:

Project::new('Project Name')->private()->for('purpose here')->with('username here');
    
16.05.2016 / 16:51