Build your next PHP project the organized way

Build your next PHP project the organized way

Everyone like to keep their things in order, so they’re easier to understand, sort through and manage. Same thing applies for web projects. When starting a php/html project, it might be a very abstruse act, figuring out how you can organize your project files the best possible way. If you’ve ever been confused with where to place your images, css, external libraries or keeping your logic files from layout or template files, then there are a few tips I have for you. If you follow this guide, It will get you headed in the right direction.

Directory Structure

If you have experience with Frameworks, they usually provide you with a directory structure to use. But in this scenario you are building a website from scratch and you need to design a strong directory structure which you could reuse in multiple projects.

Summary

www is your document root directory i.e. public_html Where all public files are accessed.  

Directory Structure
Directory Structure

Assets/css- This directory holds the CSS files.

Assets/img- This directory is for images and it has two subdirectories – content and uploads. All your images are stored in their respective directories.

Assets/js-  For Javascripts.

Classes–  All of your PHP Classes are stored here.

Configs- The Configs directory is for storing website configuration files. I’ll explain it later in the post.

Errors: Error directory has error pages i.e 404.php 403.php.

Libs- Libs directory is dedicated to helper library files.

thirdparty- If you ever need to integrate third party applications, their files go in this directory.

 


Adding a config.php file in your project.

config.php is a must-have file for any project. We design a config.php file to store site-wide settings. As a developer our goal is to do as less work as possible in an efficient way and these config files help us a lot to reach this goal.

For this project, all configuration files are stored in “configs” directory. For better understanding, check the following example.

This is a basic config.php file which contains arrays of most commonly used data and some constants. Using constants for commonly used paths for PHP require statements i.e. include or require will make your life better and if the paths ever changes or you move your application to a different host. You’ll only need to update it in one place.

Adding more config files for different environments or third party applications

Instead of adding all configurations in a single file, you should rather add multiple config files for multiple environments and applications. For example, if you need to integrate Facebook API to your project, you can add a separate config file “facebook.php” in the configs directory and store SDK files in thirdparty directory. For example


Autoloading Classes

It’s a good idea to keep your PHP classes in separate files, with one class per file. And to store our classes I assume you already have created “classes” directory as mentioned above.

Let’s say you created a class Product and Service and put these under the Classes directory like

Then, if your application needs to create a new object of these classes you would use something like

Keeping your classes separated and organized like this is a great idea. Including Class code in your application like above is fine when you are working on a small project which only has a few classes. However, if you are working on a project that has a large number of classes, then calling each class separately like following code is not very convenient.

This kind of scenario can lead to difficulties with dependencies. If one of the classes depends upon another and your application ever changes, you have to work extra hard to be sure that the connections between your classes are viable. But, with autoloading classes in PHP can help you to overcome this problem.

In past, we have been using __autoload function But it’s not recommended anymore. Now, we use spi_autoload_register  function for this purpose.  For Example

And If you’re using PHP 5.3.0 or greater, You can also use anonymous functions.

Autoloading classes using this method are just fine. However, a more efficient way to do the same job is to use this  spl_autoload_register function is to call it in an init.php file and then include this file in our application wherever you need to use them.

init.php

And in index.php or where you need to use classes.

 


 

Storing Libraries

Anything re-usable can be termed as a Library. We usually create libraries using an object oriented approach or procedural way. For this project, you have already created “libs” directory and whichever library, you write, shall go in this directory.

For example, I have created this simple redirect class

Usage:

 Your comments suggestions would be greatly appreciated.

Comments are closed.