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

Asked

Viewed 104 times

5

PHP 7 has launched a number of innovations for the PHP language. I even noticed that classes can now have keyword named methods. The strange thing is that classes cannot have, but methods can (see!)

For example, this is valid in PHP7:

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

My curiosity is, This is good or bad?

It is really an advantage to be able to declare the name of methods as being of a keyword?

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

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’s a reason for that. According to the manual this reduces the compatibility break if a new keyword is added and has been used as a 'function' such as yield in Laravel (example of the Soen problem). The other advantage is to maintain vocabulary in the creation of a DSL (Domanin specific language) that is it is not necessary to seek a synonym.

Example:

Project::new('Project Name')->private()->for('purpose here')->with('username here');
  • It would look very strange private function private() : boolean {}

  • 1

    @Wallacemaxters and the public function function()? :D

  • Very nice to have mentioned this problem of Laravel, but this problem occurred in Laravel 3, and not in 4. In 4 the problem was already solved

  • @Wallacemaxters then in the link that is the question he speaks of the 4, but I can edit without problems.

  • @Wallacemaxters leaves tomorrow after lunch to give someone time to answer :)

Browser other questions tagged

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