• About Us
    • New York
  • Work
  • Capabilities
  • Careers
  • Technology
  • Blog
  • Contact Us
  • About Us
    • New York
  • Work
  • Capabilities
  • Careers
  • Technology
  • Blog
  • Contact Us
April 18, 2016

Getting started with some PHP Best Practices

Posted by Emily Fox

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 wisely
    When 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: Autoloader
    PSR-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/ folder
    While 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 Composer
    Composer 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.

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

coding standardsPHP
Previous
Next

Latest White Papers

  • Shifting Plans for 2020 & Beyond
  • Game On: How Brands Can Log Into A Diverse Multi-Billion Dollar Industry
  • What CCPA Means For Brands
  • How Google is Improving Consumer Data Privacy
  • Ways to Prepare for the Cookieless Future
  • See all White Papers

Featured Posts

  • Ad Age Names PMG #1 Best Place to Work in 2021
  • MediaPost Names PMG Independent Agency of the Year
  • PMG Client Portfolio Trends During Amazon Prime Day 2020
  • A Closer Look at the Congressional Big Tech Market Power Report
  • What to Know About Reddit

Categories

  • Consumer Insights
  • Content
  • Creative Design
  • Data Analytics
  • Development
  • Digital TV & Video
  • Ecommerce
  • Industry News
  • Local
  • Mobile
  • Paid Search
  • PMG Culture
  • Programmatic & Display
  • SEO
  • Social Media
  • Structured Data
Fort Worth

2845 West 7th Street
Fort Worth, TX 76107

Dallas

3102 Oak Lawn Avenue
Suite 650
Dallas, TX 75219

Austin

823 Congress Avenue
Suite 800
Austin, TX 78701

London

33 Broadwick Street
London
W1F 0DQ

New York

120 East 23rd Street
New York, NY 10010

Get in touch

(817) 420 9970
info@pmg.com

Subscribe to the PMG Newsletter
© 2021 PMG Worldwide, LLC, All Rights Reserved
  • Contact
  • Privacy Policy
 Tweet
 Share
 Tweet
 Share
 Tweet
 Share
 LinkedIn
We and our partners use cookies to personalize content, analyze traffic, and deliver ads. By using our website, you agree to the use of cookies as described in our Cookie Policy.