Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP 's' in namespace: Controller or Controllers, Model or Models?

For example Symfony uses \Controller. Yii2 uses \controllers and \models.

Is there a standard about ...s|es like PSR?

like image 454
Ivan Avatar asked Dec 29 '17 09:12

Ivan


People also ask

What are the namespaces in PHP?

Namespaces are qualifiers that solve two different problems: They allow for better organization by grouping classes that work together to perform a task. They allow the same name to be used for more than one class.

When were PHP namespaces added?

In PHP version 5.3 a new feature known as namespacing was added to the language.


1 Answers

None of the PSRs specify whether to use singular or plural in namespaces. This is usually a convention by the framework. The same goes for capitalization.

Whether you can use your preferred naming convention - if you have any - in the framework of your choosing mostly depends on the framework itself and might even vary within parts of the framework. In the case of Symfony it is singular App\Controller by default, but it's easily changed to App\Controllers or App\controllers if you like as long as your routing configuration uses the correct name.

When deviating from the framework's convention you might run into problems, because of the expectations it has when configuring your app. For example Symfony 3 autoloads your Commands when they are placed in the Command/ folder and the class name is suffixed with Command. When you deviate from this you have to manually register them. On the other hand Symfony 4 uses the new autiwiring and autoconfigure defaults in the service configuration and will detect commands based on whether they implement the correct interface - either directly or indirectly by extending the base Command-class - and therefore can have whatever name you like and can be put in any folder. So even within the framework or between different versions of the framework changing the name to something other than the recommendation might have different repercussions.

like image 81
dbrumann Avatar answered Oct 18 '22 23:10

dbrumann