PHP TutorialCompile PHP ExtensionsContributing to the PHP CoreContributing to the PHP ManualCreate PDF files in PHPInstalling a PHP environment on WindowsPHP Alternative Syntax for Control StructuresPHP APCuPHP Array iterationPHP ArraysPHP Asynchronous programmingPHP Autoloading PrimerPHP BC Math (Binary Calculator)PHP Built in serverPHP CachePHP Classes and ObjectsPHP ClosurePHP Coding ConventionsPHP Command Line Interface (CLI)PHP CommentsPHP Common ErrorsPHP Compilation of Errors and WarningsPHP Composer Dependency ManagerPHP ConstantsPHP Control StructuresPHP CookiesPHP CryptographyPHP DateTime ClassPHP DebuggingPHP Dependency InjectionPHP Design PatternsPHP Docker deploymentPHP Exception Handling and Error ReportingPHP Executing Upon an ArrayPHP File handlingPHP Filters & Filter FunctionsPHP Functional ProgrammingPHP FunctionsPHP GeneratorsPHP Headers ManipulationPHP How to break down an URLPHP How to Detect Client IP AddressPHP HTTP AuthenticationPHP Image Processing with GDPHP ImagickPHP IMAPPHP Installing on Linux/Unix EnvironmentsPHP JSONPHP LocalizationPHP LoopsPHP Machine learningPHP Magic ConstantsPHP Magic MethodsPHP Manipulating an ArrayPHP mongo-phpPHP Multi Threading ExtensionPHP MultiprocessingPHP MySQLiPHP MySQLi affected rows returns 0 when it should return a positive integerPHP NamespacesPHP Object SerializationPHP OperatorsPHP Output BufferingPHP Outputting the Value of a VariablePHP Parsing HTMLPHP Password Hashing FunctionsPHP PDOPHP PerformancePHP PHPDocPHP Processing Multiple Arrays TogetherPHP PSRPHP Reading Request DataPHP RecipesPHP ReferencesPHP ReflectionPHP Regular Expressions (regexp/PCRE)PHP Secure Remeber MePHP SecurityPHP Sending EmailPHP SerializationPHP SessionsPHP SimpleXMLPHP SOAP ClientPHP SOAP ServerPHP SocketsPHP SPL data structuresPHP SQLite3PHP StreamsPHP String formattingPHP String Parsing


From WikiOD

The PSR (PHP Standards Recommendation) is a series of recommendations put together by the FIG (Framework Interop Group).

"The idea behind the group is for project representatives to talk about the commonalities between our projects and find ways we can work together" - FIG FAQ

PSRs can be in the following states: Accepted, Review, Draft or Deprecated.

PSR-4: Autoloader[edit | edit source]

PSR-4 is an accepted recommendation that outlines the standard for autoloading classes via filenames. This recommendation is recommended as the alternative to the earlier (and now deprecated) PSR-0.

The fully qualified class name should match the following requirement:

  • It MUST contain a top level vendor namespace (E.g.: Alphabet)
  • It MAY contain one or more sub-namespaces (E.g.: Google\AdWord)
  • It MUST contain an ending class name (E.g.: KeywordPlanner)

Thus the final class name would be Alphabet\Google\AdWord\KeywordPlanner. The fully qualified class name should also translate into a meaningful file path therefore Alphabet\Google\AdWord\KeywordPlanner would be located in [path_to_source]/Alphabet/Google/AdWord/KeywordPlanner.php

Starting with PHP 5.3.0, a custom autoloader function can be defined to load files based on the path and filename pattern that you define.

# Edit your php to include something like:
spl_autoload_register(function ($class) { include 'classes/' . $class . '.class.php';});

Replacing the location ('classes/') and filename extension ('.class.php') with values that apply to your structure.

Composer package manager supports PSR-4 which means, if you follow the standard, you can load your classes in your project automatically using Composer's vendor autoloader.

# Edit the composer.json file to include
    "autoload": {
        "psr-4": {
            "Alphabet\\": "[path_to_source]"

Regenerate the autoloader file

$ composer dump-autoload

Now in your code you can do the following:


require __DIR__ . '/vendor/autoload.php';
$KeywordPlanner = new Alphabet\Google\AdWord\KeywordPlanner();

PSR-1: Basic Coding Standard[edit | edit source]

PSR-1 is an accepted recommendation and outlines a basic standard recommendation for how code should be written.

  • It outlines naming convetions for classes, methods and constants.
  • It makes adopting PSR-0 or PSR-4 recommendations a requirement.
  • It indicates which PHP tags to use: <?php and <?= but not <?.
  • It specifies what file encoding to use (UTF8).
  • It also states that files should either declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or execute logic with side effects and not define symbols, but do both.

PSR-8: Huggable Interface[edit | edit source]

PSR-8 is a spoof PSR (currently in Draft) proposed by Larry Garfield as an April Fools joke on 1 April 2014.

The draft outlines how to define an interface to make an object Huggable.

Excert from the code outline:


namespace Psr\Hug;

 * Defines a huggable object.
 * A huggable object expresses mutual affection with another huggable object.
interface Huggable

     * Hugs this object.
     * All hugs are mutual. An object that is hugged MUST in turn hug the other
     * object back by calling hug() on the first parameter. All objects MUST
     * implement a mechanism to prevent an infinite loop of hugging.
     * @param Huggable $h
     *   The object that is hugging this object.
    public function hug(Huggable $h);