Yii Framework Forum: Namespaces Naming Convention - Yii Framework Forum

Jump to content

  • (3 Pages)
  • +
  • 1
  • 2
  • 3
  • You cannot start a new topic
  • You cannot reply to this topic

Namespaces Naming Convention

#21 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,902
  • Joined: 04-October 08
  • Location:DC, USA

Posted 03 April 2013 - 02:59 PM

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.
0

#22 User is offline   Chris83 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 440
  • Joined: 27-February 10
  • Location:Helsinki, Finland

Posted 03 April 2013 - 03:30 PM

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();

Best regards,
Chris

My contribution to the Yii community:
Account | Yiistrap | Auth | Bootstrap | NordCms | Rights | LESS | SEO | Img

Follow me:
Twitter | GitHub | Bitbucket
1

#23 User is offline   schmunk 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 569
  • Joined: 02-November 08
  • Location:Stuttgart, Germany

Posted 03 April 2013 - 03:52 PM

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.
Phundament - Yii Application Boilerplate with composer support
Fork on github

Follow phundament on Twitter

DevSystem: Mac OS X 10.7 - PHP 5.3 - Apache2 - Yii 1.1 / trunk - Firefox or Safari
0

#24 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,902
  • Joined: 04-October 08
  • Location:DC, USA

Posted 03 April 2013 - 04:01 PM

@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.
0

#25 User is offline   schmunk 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 569
  • Joined: 02-November 08
  • Location:Stuttgart, Germany

Posted 03 April 2013 - 04:13 PM

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

\_Yii


But may work according to PSR-0
Phundament - Yii Application Boilerplate with composer support
Fork on github

Follow phundament on Twitter

DevSystem: Mac OS X 10.7 - PHP 5.3 - Apache2 - Yii 1.1 / trunk - Firefox or Safari
0

#26 User is offline   schmunk 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 569
  • Joined: 02-November 08
  • Location:Stuttgart, Germany

Posted 03 April 2013 - 04:18 PM

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.
Phundament - Yii Application Boilerplate with composer support
Fork on github

Follow phundament on Twitter

DevSystem: Mac OS X 10.7 - PHP 5.3 - Apache2 - Yii 1.1 / trunk - Firefox or Safari
0

#27 User is offline   Müller 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 23-June 12
  • Location:Netherlands

Posted 03 April 2013 - 05:04 PM

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

2

#28 User is offline   schmunk 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 569
  • Joined: 02-November 08
  • Location:Stuttgart, Germany

Posted 03 April 2013 - 10:17 PM

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/s...ymfony-standard ... somehow similar to the solutions above.
Phundament - Yii Application Boilerplate with composer support
Fork on github

Follow phundament on Twitter

DevSystem: Mac OS X 10.7 - PHP 5.3 - Apache2 - Yii 1.1 / trunk - Firefox or Safari
1

#29 User is offline   Müller 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 23-June 12
  • Location:Netherlands

Posted 04 April 2013 - 07:33 PM

After some thinking and looking on how Zend2 and Symfony2 did, I came up with the following proposal:
https://github.com/a...ucture-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/y...omment-15932432
0

#30 User is offline   Chris83 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 440
  • Joined: 27-February 10
  • Location:Helsinki, Finland

Posted 05 April 2013 - 05:28 AM

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.
Best regards,
Chris

My contribution to the Yii community:
Account | Yiistrap | Auth | Bootstrap | NordCms | Rights | LESS | SEO | Img

Follow me:
Twitter | GitHub | Bitbucket
1

#31 User is offline   schmunk 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 569
  • Joined: 02-November 08
  • Location:Stuttgart, Germany

Posted 05 April 2013 - 05:55 AM

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.
Phundament - Yii Application Boilerplate with composer support
Fork on github

Follow phundament on Twitter

DevSystem: Mac OS X 10.7 - PHP 5.3 - Apache2 - Yii 1.1 / trunk - Firefox or Safari
2

#32 User is offline   Müller 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 23-June 12
  • Location:Netherlands

Posted 05 April 2013 - 06:05 AM

View Postschmunk, on 05 April 2013 - 05:55 AM, said:

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.
1

#33 User is offline   Haensel 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 444
  • Joined: 14-January 11
  • Location:Vienna (Austria)

Posted 05 April 2013 - 06:11 AM

View Postschmunk, on 05 April 2013 - 05:55 AM, said:

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.


Agree. +1
1

#34 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 05 April 2013 - 06:49 AM

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.
3

#35 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,902
  • Joined: 04-October 08
  • Location:DC, USA

Posted 05 April 2013 - 06:57 AM

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.
0

#36 User is offline   schmunk 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 569
  • Joined: 02-November 08
  • Location:Stuttgart, Germany

Posted 05 April 2013 - 07:08 AM

Quote

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.

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

Quote

@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.

You mean eg. "views"?

Quote

And another minor issue is: should the names be plural or not.

Haven't thought about it, but I'd tend to singular, eg. Monolog.
Phundament - Yii Application Boilerplate with composer support
Fork on github

Follow phundament on Twitter

DevSystem: Mac OS X 10.7 - PHP 5.3 - Apache2 - Yii 1.1 / trunk - Firefox or Safari
0

#37 User is offline   mbi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 608
  • Joined: 08-May 09

Posted 05 April 2013 - 07:28 AM

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/g.../ItemStore.java
https://github.com/s...rnel/Client.php
2

#38 User is offline   Chris83 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 440
  • Joined: 27-February 10
  • Location:Helsinki, Finland

Posted 05 April 2013 - 02:12 PM

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.
Best regards,
Chris

My contribution to the Yii community:
Account | Yiistrap | Auth | Bootstrap | NordCms | Rights | LESS | SEO | Img

Follow me:
Twitter | GitHub | Bitbucket
2

#39 User is offline   CeBe 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 535
  • Joined: 16-July 10
  • Location:Berlin. Germany

Posted 05 April 2013 - 02:37 PM

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...
1

#40 User is offline   Rodrigo Coelho 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 665
  • Joined: 05-August 10
  • Location:Rio de Janeiro, Brazil

Posted 05 April 2013 - 03:40 PM

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.
0

Share this topic:


  • (3 Pages)
  • +
  • 1
  • 2
  • 3
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users