Namespaces Naming Convention

I found this gist of samdark showing a base class of Yii2, and I notice the namespace names are in lower case, I hope this was abandoned. In IMHO namespace names should use the same convention as class names (StudlyCaps), i.e. SomeNamepace\SomeClass, like Symfony2 and other modern frameworks are using.

https://gist.github.com/samdark/2835928

Cheers.

Should it, why? :)

PHP encourages two naming conventions for namespaces, Camelcase and PASCAL case.

And since the names are single word, they should be all lowercase, like yii and base.

If the names were multi-names, then of course each word (after the first word) should be capitalized, like webComponent.

C++ encourages all lowercase names for namespaces (since it’s not a type!).

I think the same goes for Python.

PEAR encourages pascal or camel cased namespaces. And Samdark (or in camel-case: samDark) uses the latter. ;)

Because I think is prettier? <_<

and it would follow the PSR-1 standard.


https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md#3-namespace-and-class-names

I may not agree with all proposed standarts in PSR-*, but it sure makes easier to maintain a source code well formatted and to contribute with Yii2, since even PHPStorm (which has been told is used by many Yii core developers), has a option to auto-format using these standards.

Because a namespace is not a type?

Camelcase is perfectly valid - and encouraged - by the PHP community.

What the heck is PSR-1 ?

https://github.com/php-fig/fig-standards

That guide doesn’t actually dictate how namespaces are named, just that ‘Alphabetic characters in vendor names, namespaces, and class names may be of any combination of lower case and upper case’. :)

It does dictate how class names and class constants should be named. Namespaces should just follow psr-0.

Thanks for the link, btw. :)

Looks like a great guide.

PSR-0 is a good one. PSR-1 and PSR-2 are good as well but only as a base for your own standard since these are very subjective.

While we are looking at standards:

I assume Yii will have a built in logging functionality. Will it follow PSR-3 or wil it have its own interface?

If yii follows psr-3 and uses composer I hope that we will get the "real" interface aviable:

Its own.

Any reason for this? Anything you plan to implement that couldn’t be done following the PSR? Or is there anything you don’t like about it?

To me, it looks very similar to the current CLogger (looking only at the interface).

  • the log level specific helpers are missing in CLogger

  • they define other log levels

  • the context array is missing in CLogger, but shouldn’t be hard to implement. The interpolation it provides seems handy, I often find myself writing log messages like “Something happened, reason was: ‘{$reason}’.”

  • the category is missing in the PSR. Not sure how this can be solved, maybe by using several logger instances (one per category).

The category is the main showstopper here.

Did you already decide how to access the logger functionality? Will it remain a static helper, or will there be instances? A quick search yesterday showed, that both log4php and monolog support a concept similar to categories (not sure if they can be nested though) and support or plan to support PSR-3.

Even if you don’t plan to use PSR-3 interfaces directly, it might be worth using an abstract logger class or an interface, so one can easily implement an adapter to a PSR-3 compatible logging framework.

The design of the logger is already finalized and implemented. We will not follow PSR-3.

Our logger is not a static helper (similar as in Yii 1.1). It just happens that the logger interface is exposed via Yii like static methods.

Sure, sorry for that inaccuracy. ;)

In fact, I very much like the way yii allows us to configure log routes, filters and stuff. Not sure if I would switch to another logging framework even if I could. Also, it’s hard to argue without knowing what you’re coding.

And after reviewing how the logging currently works, I think it would be even possible to inject a 3rdParty logging framework in Yii 1.1. It’s basically the LogRouter that needed to be exchanged with an adapter.

Thank you for understanding. If you have specific improvement suggestions, please let us know.

Hi Qiang and Alex,

I’ve read through this thread and found that the OP’s question is not really answered.

As PSR-0 does not dictate to use uppercase first letters for namespaces, but virtually every PHP project with namespaces - I’ve looked so far - uses UpperCase\FirstLetters, I really would like to know if \Yii ;) will adopt this format also?

I would also suggest it, because it’s like an unwritten convention now.

Best regards,

\Schmunk

I’m actually debating about both choices. If we choose to use CamelCase, how should we name the Yii static class? We would like it to be \Yii, which would confuse users with the \Yii namespace.

If I get it right like, you’d have something like:


echo \Yii:t()

$obj = new \Yii\Base\Object();



Why would it confuse users? I would say the syntax prevents misuse.

I’d actually like it ;)

How do others deal with it?

I mean the options aren’t that much, it would have to be either \YII or \yii if not the above.