Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a well-established naming convention for PHP namespaces?

So far, I've seen many different naming conventions used for PHP namespaces. Some people use PascalCase\Just\Like\For\Classes, some use underscored\lower_case\names, some even use the Java convention for package names: com\domain\project\package.

The question is very simple -- can any of these (or other) conventions be called well-established? Why? Are any of them recommended by authorities like Zend or the developers of well-known PHP frameworks?

like image 924
Ignas R Avatar asked Nov 30 '09 15:11

Ignas R


People also ask

What are the naming conventions in PHP?

Variable naming rules PHP First, every variable must start with the special character $ . Variables can then start with an underscore ( _ ) or any letter, but not with a number or a special character. The names of your variables cannot contain special characters such as & , % , # , or @ .

What is PHP name space?

Like C++, PHP Namespaces are the way of encapsulating items so that same names can be reused without name conflicts. It can be seen as an abstract concept in many places. It allows redeclaring the same functions/classes/interfaces/constant functions in the separate namespace without getting the fatal error.

What is true about namespace 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.

Is PHP namespace case sensitive?

Note: Namespace names are case-insensitive. Note: The Namespace name PHP , and compound names starting with this name (like PHP\Classes ) are reserved for internal language use and should not be used in the userspace code.


2 Answers

A PHP Standards Working Group made up of contributors to many different PHP frameworks and projects came up with the following proposal for appropriate namespace usage:

https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md

like image 78
ramsey Avatar answered Sep 27 '22 20:09

ramsey


I don't think you can say there is anything well established at this point, but there is an RFC discussing it for PEAR2: http://wiki.php.net/pear/rfc/pear2_naming_standards

My take is that namespaces are a form of variables, and therefore should follow the same convention as they do. I genereally prefer lowercase_underscore for free variables (As opposed to object members), so the most natural for my taste would be namespace\ClassName. This also matches the most prevalent conventions from other languages. On the other hand, the same argument could be made for pre-5.3 pseudo-namespaces, but here the de-facto standard seems to be using CamelCase.

like image 35
troelskn Avatar answered Sep 27 '22 18:09

troelskn