Yii Framework Forum

Lowercase or CamelCase namespaces?


(Alexander Makarov) #1

Historically Yii used lowercase namespaces such as:

namespace yii\caching;

While it conforms to PSR coding style guides (they do not regulate it), majority of PHP packages use CamelCase namespaces such as:

namespace Yii\Caching;

That, when many packages are used in Yii project, causes naming inconsistencies.

Which of the following do you prefer?

  • Keep lowercase namespaces
  • Switch to CamelCase namespaces

0 voters


(Mehdi Achour) #2

I really love the lowercase form, but since the repo-split for Yii 3, I don’t like having \yii\activerecord for a package named active-record.


#3

Note that Yii namespace conflicts with Yii class. yii does not.


(Fabrizio Caldarelli) #4

Although lowercase is nice, the question is really about multiple words namespace. As @machour said, we are forced to write namespaces linking words without spaces.

So: Yii\ActiveRecord or yii\activerecord ?

I prefer the first.

P.S. Otherwise yii\activeRecord ?


(Alexander Makarov) #5

@CeBe potentially we’ll be able to get rid of Yii::.


(Lubosdz) #6

Then would be alternative syntax?


#7

For me CamelCase namespaces are historical leftovers from PSR-0 autoloading rules, where namespace was part of the class name (so it should follow class name naming conventions) and change from SomeVendor_SomeClass to SomeVendor\SomeClass was natural. But there is no real reason to promote CamelCase namespaces, while there are reasons to not use it (it is easier to distinguish between namespace and class name when they follow different naming conventions). Changing this for Yii gives no benefits, only increases the amount of work needed to upgrade from Yii 2 to Yii 3.


#8

:+1: this does not only apply to yii vs. Yii but also to e.g. yii\db\QueryBuilder as the class but there may also be a yii\db\querybuilder namespace that contains helper classes.

I do not see any problem with current namespace naming, what are the reasons to change it?


(Sizemail) #9

Lowercase in namespaces more familiar. Most of packages from packagist.org use lowercase. I think it’s easy to perceive use section when every item has the same style.


(SohelAhmed) #10

Till what time this poll will remain open? I mean to say when can we see the finally accepted way for namespace


#11

Decision is not going to be made by the poll result, poll should only give an idea of overall preference. Decision should be made based on arguments given.


(Alexander Makarov) #12

I think most of packagist packages use CamelCase namespaces, not lowercase.


(SohelAhmed) #13

I personally prefer lowercase (or snake_case) everywhere it is possible for its better readabilty and ease of typing


(Schmunk) #14

As said before, lowercase directory names and namespaces make it much easier to distinguish what’s a namespace and what’s a Class.


(Brandon) #15

CamelCase for consistency with other popular packages & frameworks, and because snake_case for multiple words is ugly.


(yuniorsk) #16

this may not be as important, but php docs also uses camel case in its examples, so it may be a bit confusing for new yii adopters, also concerning multiple word I am in favor of camel case


(Jacob Moen) #17

I’d like to keep lowercase for namespaces. For ease to use and PSR conformity.
That the PHP website uses CamelCase is their problem :slight_smile: They probably did it before lowercase became the thing, and then keep doing it because they didn’t think to change it.

If Yii goes with CamelCase, then we’ll see naming conflicts, making it both more cumbersome to type and to use.


(Viktor) #18

I’d prefer CamelCase because

  • most of packagist.org packages use it
  • it’s MuchMoreReadable than somecoolnamespace

#19

The alternative is someCoolNamespace - it should not be less readable than CamelCase since (probably) most of names in your code already follows this convention.


(Alexander Makarov) #20

Both lowercase and CamelCase are PSR-compatible. Also I think that CamelCase is used more than lowercase nowadays.