SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Location
    Singapore
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Namespaces, Filenames and Autoload

    Hi all, I have started work on my new PHP web app and before getting too far would like to ensure I am following best practices for file naming and namespaces so that I can use an autoload function effectively.

    I have read up on PSR-0 and have a question: What would the vendor name be in an web application I am building, simply the project name?

    Additionally, I currently have the following structure (no namespaces and my class names match the filenames), I'd like to work out the best way to name the folders, filenames, namespaces and classes?

    Code:
    myProject.com
      /app
        /core
          /BaseController.php
          /BaseMapper.php
          /BaseModel.php
          /Loader.php
        /controllers
          /BookController.php
        /datamappers
          /BookMapper.php
        /models
          /domain
            /Book.php
          /BookModel.php
        /views
          /Book
            /List.php
            /Details.php
      /public
        /index.php
    • Should the namespace for BaseController be MyProject\App\Core, or
    • Should the namespace for BaseController be MyProject\App and the class name Core_BaseController?
    • Should I rename my folders to all use upper camel case? (eg. App, Core, etc)
    • Should my class names remain in upper camel case?
    • Should my directories all be singular or plural? Does it matter?


    Lastly, I have had a look at the recommended SplClassLoader here: https://gist.github.com/jwage/221634

    If I follow the above standards would I then need to register with:

    PHP Code:
    $classLoader = new SplClassLoader('MyProject\App\Core''../App/Core');
    $classLoader->register();

    $classLoader = new SplClassLoader('MyProject\App\Controllers''../App/Controllers');
    $classLoader->register();

    $classLoader = new SplClassLoader('MyProject\App\DataMappers''../App/DataMappers');
    $classLoader->register();
    ... 
    This seems like horrible overkill. Can this be done more efficiently?


    Thanks,
    Jordan
    Last edited by Jordan Windebank; Dec 30, 2013 at 01:43. Reason: Fixed slash direction in namespace examples.

  2. #2
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,246
    Mentioned
    16 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jordan Windebank View Post
    What would the vendor name be in an web application I am building, simply the project name?
    Yup. Or the client name. Or a combination of the two.

    \ClientName\ProjectName\


    Quote Originally Posted by Jordan Windebank View Post
    I'd like to work out the best way to name the folders, filenames, namespaces and classes?
    The most common structure starts with two folders:

    Code:
        src/
        vendor/
    "src" is where you put the code that you write specifically for this project. "vendor" is where you put third-party code and libraries.

    Within "src", your folder structure would match your namespaces.

    Code:
        src/
            ClientName/
                ProjectName/
                    Controller/
                    DataMapper/
                    Model/
                    View/
        vendor/
    I think "core" might be too general of a name. It seems like you're putting base classes of all kinds in there. It would probably be better if BaseController lived in the Controller namespace, BaseMapper in the DataMapper namespace, and so forth.

    Quote Originally Posted by Jordan Windebank View Post
    [*]Should I rename my folders to all use upper camel case? (eg. App, Core, etc)[*]Should my class names remain in upper camel case?
    It's most important that your folder names -- case and all -- match your class and namespace names -- case and all -- because file systems can be case sensitive. With that being said, the prevailing standard for class and namespace names is upper camel case, which means that, yes, your folder names would also be upper camel case.

    Quote Originally Posted by Jordan Windebank View Post
    [*]Should my directories all be singular or plural? Does it matter?
    Mostly it doesn't matter. Though, the PHP community seems to have settled on singular for namespace names (and thus also for folder names).

    Quote Originally Posted by Jordan Windebank View Post
    Lastly, I have had a look at the recommended SplClassLoader here: https://gist.github.com/jwage/221634

    If I follow the above standards would I then need to register with:

    PHP Code:
    $classLoader = new SplClassLoader('MyProject/App/Core''../App/Core');
    $classLoader->register();

    $classLoader = new SplClassLoader('MyProject/App/Controllers''../App/Controllers');
    $classLoader->register();

    $classLoader = new SplClassLoader('MyProject/App/DataMappers''../App/DataMappers');
    $classLoader->register();
    ... 
    This seems like horrible overkill.
    Using the new structure above, you should only need:

    Code:
    $classLoader = new SplClassLoader('ClientName\\ProjectName', '../src');
    $classLoader->register();
    "First make it work. Then make it better."

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Location
    Singapore
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Jeff, going to have a play with this now. Appreciate your advice.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •