Home » Php » php – Folder/File Structure Conventions?

php – Folder/File Structure Conventions?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’m trying to find a guide on PHP file/folder structure conventions.

I’m using GitHub and want to ensure I’m following a standard convention as to not confuse users.

Any help would be appreciated.

How to&Answers:

As speshak said PHP has no standards and no conventions (including its own standard library).

However:

  1. In public directory (usually public_html) store only static resources (images/JS/CSS) and one PHP file index.php which is limited to something like this:

    <?php
    
    
    require '/path/to/app/outside/public/html/start.php';
    $app = new App();
    $app->run();
    
  2. Application itself should be stored outside public directory.

  3. File structure might reflect classes names, so: Project\Util\XML\Parser will be stored within /path/to/the/project/Project/Util/XML/Parser.php file.
  4. Of course 3rd-party-code might be stored in separated folder, let’s say vendor – that’s quite common convention.

Answer:

There is this:

https://github.com/php-pds/skeleton

Based on data collected from various open-source projects, this document proposes a naming convention for a number of root-level directories designated for various common purposes, as well as naming conventions for root-level documentation files.

I didn’t find that this covers every imaginable scenario, but it’s nice to be able to point at this and indicate you’re making an effort to achieve some consistency across projects 🙂

Answer:

You are free to choose any directory structure. But if you would like to know about best practices, take a look at how it is done in frameworks, for example, symphony.

Take a look: http://www.flickr.com/photos/deia/402335716/

Here’s few of them:

  • All the code that is included, should be put outside of the document root.

  • HTML templates should be in a separate directory.

  • Libraries and classes should be in ‘lib’ directory.

Mostly it’s just a reasonable solutions, not strict conventions.

Answer:

I think the most obvious one is your libraries. You should name your classes like YourCompany_Module_Class, if you want to be compatible. Using this standard, your libraries can be used along with any other similarly named libraries without clashes and problems. The new namespacing in PHP 5.3+ helps more in achieving this. You can have some guidelines for this at Zend Coding Standards – File Naming and at PSR-0 Standard Proposal.

Other than that, you’d better constantly keep the future in your mind and plan your folder structure accordingly. For example, let’s say you are uploading images into user_images. Ok. But what happens when the project catches or gets bigger and now you have tens of thousands of files in a single folder. You must build some scheme that enables you to store ~1k images per directory at most like 12/56/154.jpg.

You will see many such problems and opportunities over time. But you can look at the current projects and learn from them for free 🙂

Answer:

PHP really has no standard. If you are making use of some framework (eg CakePHP, Zend Framework, etc) it may impose some standard on you.

If you aren’t using a third party library that forces a structure, just use common sense. (Put images in a images directory, included files in an includes directory, etc) People that download and install PHP apps will already be used to each app doing things differently. The fact that you’re giving it some thought puts you a head of lots of the competition 🙂