Namespaces Naming Convention

Just thinking loud …


use \yii\base as \Yii\Base;

use \yii\web as \Yii\Web;

...



How much Performance would that cost and would it affect auto-loading?

@Cebe

My example was a lazy one, without much thinking. I came up with this one later:

4084

php-app-structure-proposal.png

I posted the link in this topic before. Here it comes again:

https://github.com/andersonamuller/php-app-structure-proposal

The convention for directories is the singular form.

For directories containing non classes files the convention is all lower-case separating words with a dash.

For namespaces directories containing classes files the convention is StudlyCaps.

The examples I see here are with namespaces with just one word which doesn’t look awful. IMO, it doesn’t look good when you start to have mixing cases.


$demo = new \MyApp\TestDrive\SuperDemo();


$demo = new \myApp\testDrive\SuperDemo();

I don’t see how camelCase can avoid problems when deploying in file systems that are case sensitive. I saw people doing this kind of mistakes with whatever convention we may think of. Unless we introduce a new convention where everything is lowercase and write a super new awesome autoloader from the ground.

And we should also avoid arguments making use of the excuse of less tying. I think we all agree that any code is clearer when written the closest to a human readable language even though we are writing for a machine interpretation.

Tá combinado? ;)

P.S. It is hard to debate in your non-native language :huh:

Two different styles seem inconsistent.

What is the advantage? I don’t see it… Don’t you also know if a file contains PHP classes by the directory structure?

Guys, count me in on the pro-lowercase side.

I just stumbled upon this code:


namespace Codeception\Module;

class WebHelper extends \Codeception\Module

And as @Qiang mentioned, it is really confusing. ::)

Good that you are convinced by yourself. :)

@Alexander

But, I think they must put it as "MUST BE" for a reason, if it is not necessary, then it should be put as "SHOULD" not "MUST". I wonder why they do so. Since we are also a member of FIG, maybe we could propose to change this rule, if the rule does not make sence.

But I somehow feels like that we should follow the law, when the congress has not modify it.

Sorry to bring this ancient topic up, just because that a few developers keep pointing this out.

Which MUST BE you’re referring to? PSR-1 says namespaces should follow PSR-0 which says: “Alphabetic characters in vendor names, namespaces, and class names may be of any combination of lower case and upper case.”.

When pointing this out, please point to and quote what you are referring to, otherwise a discussion is impossible.

There is NO PSR telling to use camel case.

The last time I saw this discussion here: 7 Reasons to Choose the Yii 2 Framework started by Peter Petermann

Yes, I know there are no PSR says anything about using camel case in namespaces, it is referring to Class Name.

However, He said that:

That’s why I think it maybe a misunderstanding? But on the other hand, the PSR-1 also do not clarify this well.

LOL, according to https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md#3-examples

The author does prefer to using camel case in namespaces, and does lead to an inconsistent folder structure. Don’t you think it is a bit wired? ;)

schmunk, your point I think is quiet correct.

Except for that, Consider also the following scenario:

use Name1\Name11\Name111;

use Name2\Name21\Name211;

Where in the first line we are importing a Class name (Name111), while on the second, a namespace (Name211).

It is obvious that by reading these two lines, you cannot figure out where’s the class and where’s the namespace. And imagine the case of more “use” statements. This is a completely wrong approach in my opinion, which also makes even coding a bit harder. Not to mention that having two different types so close they must be named in a different way.

I see a lot of frameworks following the PascalCase practice for namespaces, but I still haven’t found any real reason for doing this (“Because they do it” of course is not a reason). I would not have problem following them, but as I see there are REAL reasons for NOT doing it.

For easier coding and reading, lowercase in namespaces is the right way for me also.