Namespaces Naming Convention

Another problem would be that the naming of directories will be mixed.

For example, for models directory, you would need to name it as "Models" so that your model classes could be namespaced as "\My\Models\Post".

However, for other directories not storing PHP classes, you don’t want to do so in general.

Here’s my fifty-cents:

Usually namespaces follow the same naming conventions as the folder structure. I don’t know what you’ve have planned for the folder naming conventions in yii2 but I think that it would be great if it followed the same naming conventions as it’s predecessor. In other words it would be something like:




echo \Yii::t();

$obj = new \yii\base\Object();



Many projects use a /src folder now to store their PHP classes, usually the autoloader maps this directory.

I think your example applies for the application directory (currently “protected”), “public” files are separated anyway and don’t have to follow these conventions.

At the moment I can think i.e. example of uploaded data and non-PHP scripts which go also into “protected”, but maybe it’s time to refactor the protected structure.

I know that the existing one is a very slim implementation and very useful for running your app out-of-the-box from your default webserver root, but I won’t sacrifice so much for it.

@schmunk: This has nothing to do with "protected" or "src".

How would you name your view files and view directories? And what about data directories, etc.?

@Chris83: The current implementation of Yii 2 uses the namespace format like you wrote (same as Yii 1.1). Here we are debating whether we should adopt CamelCase in namespace in Yii 2. So far, I’m most concerned with the mixed use of cases in directory names: class directories use CamelCase, while non-class directories use lower-case, which doesn’t look good to me.

Haven’t tried, looks weird and just for the sake of completion (not as a proposal):


\_Yii

But may work according to PSR-0

You can name them any way you want, since they don’t contain classes.

Personally I don’t see the mixture as a very crucial point, because it would also indicate a different usage of the contained files.

Would it be something like this?




root

  app

    config

      application.php

    theme

      bootstrap

        views

          site

            home.php

            ...       

  src

    Component

       Form.php

       ...

    Controller

       Site.php

       AnotherController.php

       ...

    Model

       Site.php

       AnotherModel.php

       ...

    Module

       Shop

         Controller

           Product.php

           ShoppingCart.php

           ...

         Model

           Product.php 

           ShoppingCart.php

           ...

         Resource

           views

             product

               index.php 

               ...

             shopping-cart

               summary.twig

               ...

    Resource

       assets

         css

           site.css

         js

           site.js

       views

         site

           home.php

           another-page.tpl

         another-controller

           index.twig

    ...

  vendor

    yii

      src

        Application

          Console.php

          Rest.php

          Web.php

          ...

        Base

          ...

        Component

          ...

        ...

  www

    assets

      ...

    index.php



A slight modification from my current implmentation, very similar to Müller’s.

I actually start liking the idea of Uppercase names for folders which (only) contain PHP classes in a namespace.

I think it even helps understanding how a application works.




.

├── app

│   ├── config

│   ├── data

│   │   └── upload

│   ├── messages

│   │   └── de

│   ├── runtime

│   ├── tests

│   └── themes (views)

│       ├── backend

│       └── frontend

├── src

│   ├── Commands

│   │   └── views

│   ├── Components

│   ├── Controllers

│   ├── Extensions

│   ├── Models

│   ├── Modules

│   └── Phundament

├── vendor

│   └── yiisoft

│       └── yii

└── www

    ├── assets

    ├── images

    ├── runtime

    └── themes

        ├── backend

        └── frontend



app and src could also be combined.




.

├── app-src

│   ├── Commands

│   │   └── views

│   ├── Components

│   ├── config

│   ├── Controllers

│   ├── data

│   │   └── p3media

│   ├── messages

│   │   └── de

│   ├── Models

│   ├── runtime

│   ├── tests

│   └── themes (views)

│       ├── backend

│       └── frontend

├── vendor

│   └── yiisoft

│       └── yii

└── www

    ├── assets

    ├── images

    ├── runtime

    └── themes

        ├── backend

        └── frontend




Here’s how Symfony does it: https://github.com/symfony/symfony-standard … somehow similar to the solutions above.

After some thinking and looking on how Zend2 and Symfony2 did, I came up with the following proposal:

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

It could be better documented, but I will try to do it shortly, depending on the feedback from others.

Since this is also related to the skeleton application, I posted a comment where I judge is a proper place:

https://github.com/yiisoft/yii2/issues/40#issuecomment-15932432

Does PSR-0 require the folders containing classes are named using CamelCase? If not, I’d like to keep it as it is currently in Yii, I’m not too fond of the namespace naming conventions used by Zend2 and Symfony2.

@Qiang: I don’t think Yii 2 should use CamelCase for namespaces unless it’s absolutely necessary. I share your concerns in this matter.

Is there another (larger) project which uses headlessCamelCase for namespaces?

IMHO it would be really annoying, when working with namespaces in Yii2, that the namespace syntax would be mixed all the time.




$obj = new \yii\base\Object();

$foo = new \Another\Base\Object();


use Composer\Script\Event;

use yii\console\script\Event;


use \Monolog

use \yii\log\Logger;



:wacko:

Btw: PSR-0 uses only CamelCase in it’s examples.

I share your pain :(

I would like to also point out that this is the time for this kind of changes.

Leave as it is, so it looks familiar with the current implementation should not be a big concern.

Yii2 is jumping to the next generation and I have high hopes that it is going to set the new top standard.

Agree. +1

I wonder if namespaces in lowercase are really such a big issue. If uppercase namespaces require us to have some directory names start with an upper case letter then we should really avoid them. On linux systems (which most of us will use for professional hosting) it’s very unusual to have such a directory name. And they are more inconvenient to type.

I’m not convinced yet.

I’m not totally against using CamelCase in namespace because it has its pros as you explained.

But I’m very bothered by the inconsistent naming in directories, and also as Mike wrote, it’s uncommon to use upper case in directory names.

@schmunk: even if the directories are organized as you proposed, there are still cases that directories are named differently under the same directory, which is confusing. And another minor issue is: should the names be plural or not.

I think that has been changed for many PHP projects, eg. Composer.

You mean eg. "views"?

Haven’t thought about it, but I’d tend to singular, eg. Monolog.

I vote for:

  • lowercase directory names

  • CamelCase class names

  • plural base folder names (components, models, extensions etc)

imho it doesnt violate psr-0

compare

https://github.com/github/android/blob/master/app/src/main/java/com/github/mobile/core/ItemStore.java

I’m still for camelHump namespaces and folder names. I don’t think that it’s a problem if Symfony components use different naming conventions.

Besides I like the current folder naming conventions in Yii, both the casing and plural form. I kind of dislike the CamelCase singular directory names, it feels a bit less natural.

IMO adding a convention that uses UpperCase directory names if the folder contains classes and lowerCase if not adds some unnecessary confusion when you create directories.

On file systems that are case sensitive it may cause more problems than before. I’ve seen many people deploying code from a windows machine to a linux server and wondering why it did not work there.

I haven’t heard really good arguments except “All the other do it like this.” for CamelCase namespaces…

I think we can agree that namespaces should reflect the directory structure and so lowerCase directory names will result in lowerCase name spaces. As Mike pointed out it is very uncommon to name directories in CamelCase on a linux system. On a terminal to create a directory or to go into you always have to remember to type the right case when you want to enter a directory. This is a bit annoying when you have CamelCase but even more when you have a mixed structure.

Unless there is a really good reason we should not use a possibly confusing convention.

Btw, Müller: Why is "Resource"-directory UpperCase in your example? it does not contain any classes…

I’ve been reading this discussion since the beginning and I’m with the guys who prefer lowercase for directory names and lowercase for namespaces.

Mike, Chris83 and CeBe have outlined the best arguments. Plus, the less we have to press [shift], the better.

For singular x plural names for the directories, I’m slightly in favor of singular, but either one is good.