This website uses cookies to ensure you get the best possible experience. See our Cookies Policy.

PMG Digital Made for Humans

Getting started with some PHP Best Practices

2 MINUTE READ | April 18, 2016

Getting started with some PHP Best Practices

As we code we all develop our own personal way of writing code, however there are a few best practices we should always follow. Following these practices not only makes it easier for others to read your code, but also easier for future you to understand what is going on. Here is a list of some of the standards you should follow when writing PHP:

  • Pick your application name wiselyWhen creating a new application or package, it’s important you pick the name of it wisely, doing a little research beforehand to see whehter the name is already taken. This will help other people find your app easily. For example, Twig is an existing and popular PHP templating engine, calling your app “twig” would not be a good idea.

  • Use PSR-4: AutoloaderPSR-4 is a coding standard recommended by PHP-Fig (Framework Interop Group). The standard outlines specifications for autoloading classes. It includes things like you MUST include top-level namespaces and all class names MUST be referenced in a case-insensitive fashion. You can read the full list of specifications here.

  • Follow a coding style (PSR-1 and PSR-2)PSR-1 is a basic coding standard. PSR-2 is a coding style guide which extends and expands on PSR-1. Following these guidelines ensures your code is easy to read, add to, and modify. It keeps your code consistent across all your projects, along with existing PHP open source projects as they follow these standards too. The standards specify things like where you should include blank lines, using 4 spaces rather than tab indentation, and where to place your curly brackets after methods vs. expressions.

  • Put your application code in an /src/ folderWhile putting your application source code in an src folder is not a requirement, it is a common trend. By following this it lets other developers know exactly where to go which is very helpful. For example, your project root will look similar to the following:

  • Use ComposerComposer is a Dependency Manager for PHP. If you’re creating a package/library, you should distribute it via Composer. Similarly, if you’re creating an application you should require your dependencies using Composer. If your project depends on a library you have declared using Composer, it will manage the update and install of them for you.

Insights meet inbox

Sign up for weekly articles & resources.

These are just a few of the PHP best practices, you can find more on PHP-FIG and the PHP Package Checklist.


Posted by Emily Fox

Related Content

thumbnail image

Get Informed

PMG Innovation Challenge Inspires New Alli Technology Solutions

4 MINUTES READ | November 2, 2021

Get Informed

Applying Function Options to Domain Entities in Go

11 MINUTES READ | October 21, 2019

thumbnail image

Get Informed

My Experience Teaching Through Jupyter Notebooks

4 MINUTES READ | September 21, 2019

Get Informed

Trading Symfony’s Form Component for Data Transfer Objects

8 MINUTES READ | September 3, 2019

Get Inspired

Working with an Automation Mindset

5 MINUTES READ | August 22, 2019

Get Informed

Parsing Redshift Logs to Understand Data Usage

7 MINUTES READ | May 6, 2019

Get Inspired

3 Tips for Showing Value in the Tech You Build

5 MINUTES READ | April 24, 2019

thumbnail image

Get Informed

Testing React

13 MINUTES READ | March 12, 2019

Get Inspired

Tips for Designing & Testing Software Without a UX Specialist

4 MINUTES READ | March 6, 2019

Get Informed

A Beginner’s Experience with Terraform

4 MINUTES READ | December 20, 2018

BACK TO ALL POST