Over a million developers have joined DZone.

How to Properly Structure Your PHP Projects

DZone's Guide to

How to Properly Structure Your PHP Projects

If you're looking to get into PHP development, or want a quick refresher, check out these tips on structuring your PHP web projects.

· Web Dev Zone ·
Free Resource

Jumpstart your Angular applications with Indigo.Design, a unified platform for visual design, UX prototyping, code generation, and app development.

One of the first things you learn as a PHP developer is the use of frameworks, such as the popular CakePHP and Symfony. As you use these formats throughout your career as a PHP developer, you will notice that sometimes it can be quite difficult to get what you want out of them. When you have a specific project or idea in mind that you want to create, you may not find a construction that will fit in.

As a developer, you will not have the time to sort through hundreds of different PHP structures to find the one that best fits your projects. And if you do decide to go with a framework, you could end up with resources that you do not need. In return, this will give you a bloated structure that could cause performance issues and slow down maintenance.

What Makes Good Structure

The first step we would recommend is looking at how you can separate your business logic away from everything else. Most PHP developers like to mix their logic along with the presentation, but this can cause a lot of issues in the long run. Especially if you’re working on a team of developers, and they need to review your code. If you aren’t working in a team, eventually you will need to return to your code to add features or fix bugs and having a clean readable code will help you finish your projects faster.

You also have to keep in mind that someday your application could become popular and could be viewed by millions of users all over the world. Using a translator class to translate your code to a worldwide audience would also help even if you don’t plan on using it. We would still suggest that you keep one in your code just in case.

PHP Templating Engine

Using a template engine with your all your projects will save you a lot of time. One of the top templating engines out on the market is called Blade. Blade is one of the most powerful template engines on the market today and it even comes built into the Larvavel 4 framework.

Another popular template engine that you can use is called Mustache. Mustache also comes in any programming language you can think of because of how easy it is to use. It is also advertised as a non-logic templating engine, which means that it will only contain the minimum files required to create the templates you will need for your projects.

Benefits of Using Templates

The biggest strength that comes out of using templates is that they help separate your business logic from your presentation. A template's responsibility is to display organized files and content that is readable to the user. Templates are, however, not responsible for any other complex task such as searching or the manipulation of your code base.

In a way, the template engines will force you to have a cleaner and more organized code, and that, as we all know, can be very important. Templates are also perfect for code reuse since they’re placed into the “views” folder. From the views folder, you can break up large pieces of code into small reusable bites, the best example of this is the header and footer of a page.

Proper PHP Structures

Let's take a look at an example template structure that was generated by Blade:

  • Source File
    • _config
    • config.environment.php
    • config.settings
  • _includes
    • blade
    • class.translator.php
  • _js
    • jquery
  • Template
    • _cache
    • _css
    • _translations
    • index
  • pages
    • index.php
    • .htaccess

The Config folder will be your primary file for your main components such as config variables, databases, classes etc. If any of your PHP string contains any configuration variables then they should be stored in the config file which you can see in the above structure. If you’re using any local or production databases then you would store those in the “config.environment.php” file.

You will also notice that we’ve included the translator class within this template in order to translate any code for future users as we’ve mentioned above. When it comes to your JavaScript files, you can clearly see that we’ve also created a folder for that which is called “_js”. In this one, you should add the latest jQuery file and all other JavaScript files.

Special Uses With the Config File

Making use of your config file is also another important concept that all PHP programmers should learn. If you’re using multiple environments such as local and production environment, then it is highly recommended that you use config files for each database. Once you have your config files setup for each database then you can switch from one environment to another on the fly without having to change any actual code or cause errors within your application.


To get the best out of your PHP structure, it is recommended to use a templating engine. You could always make your own structure from scratch but, this is not recommended since it can be very time consuming and you could cause problems for yourself in the long run. The point of template engines is that they will give you the freedom to design your structure as you see fit for your project’s goals without complexity.

You could also go for a well-known framework, like CakePHP, but, this, in general, is very broad when it comes to projects that have specific requirements. If your project ends up being something that requires multiple features that you could find in a system then, it is definitely best to go with the one that will meet your needs.

Take a look at an Indigo.Design sample application to learn more about how apps are created with design to code software.

web dev ,php ,web application development

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}