23
PSR: Standards in PHP

PSR: Standards in PHP by Alex Simanovich

Embed Size (px)

Citation preview

PSR: Standards in PHP

Story

PHP Standards for Writing CodePEAR

Zend Framework

Drupal

Mine standards

… etc

Framework Interoperability Group - FIG◦ Michael Dowling (AWS SDK for PHP)

◦ Larry Masters (CakePHP)

◦ Guilherme Blanco (Doctrine)

◦ Larry Garfield (Drupal)

◦ Filipe Guerra (SugarCRM)

◦ Bernhard Schussek (Symfony2)

◦ Alexander Makarov (Yii framework)

◦ Pádraic Brady (Zend Framework 2)

FIG’s Goal

The goal of the FIG is to create a dialogue between project representatives, with the

aim of finding ways to work together (interoperability)

PSR-0AUTOLOADER STANDARD

Toolsrequire() in PHP 4

__autoload() in PHP 5

spl_autoload() and spl_autoload_register() in PHP 5.1.2

Requirements◦ A fully-qualified namespace and class must have the following structure

\<Vendor Name>\(<Namespace>\)*<Class Name>

◦ Each namespace must have a top-level namespace ("Vendor Name").

◦ Each namespace can have as many sub-namespaces as it wishes.

◦ Each namespace separator is converted to a DIRECTORY_SEPARATOR when loading from the file system.

◦ Each _ character in the CLASS NAME is converted to a DIRECTORY_SEPARATOR. The _ character has no special meaning in the namespace.

◦ The fully-qualified namespace and class is suffixed with .php when loading from the file system.

◦ Alphabetic characters in vendor names, namespaces, and class names may be of any combination of lower case and upper case.

Example◦ \Doctrine\Common\IsolatedClassLoader =>

/path/to/project/lib/vendor/Doctrine/Common/IsolatedClassLoader.php

◦ \Symfony\Core\Request => /path/to/project/lib/vendor/Symfony/Core/Request.php

◦ \Zend\Acl => /path/to/project/lib/vendor/Zend/Acl.php

◦ \Zend\Mail\Message => /path/to/project/lib/vendor/Zend/Mail/Message.php

◦ \namespace\package\Class_Name => /path/to/project/lib/vendor/namespace/package/Class/Name.php

◦ \namespace\package_name\Class_Name => /path/to/project/lib/vendor/namespace/package_name/Class/Name.php

PSR-0: not for PHP core

PSR-1BASIC CODING STANDARD

Basic Coding Standard◦ Only use the <?php and <?= tags.

◦ Only use UTF-8 without BOM for PHP code.

◦ Separate side-effects (generate output, access a database etc.) and declarations.

◦ Enforce PSR-0.

◦ Class names must be defined in StudlyCaps.

◦ Class constants must be defined in upper case with underscore separators.

◦ Method names must be defined in camelCase.

PSR-2CODING STYLE GUIDE

Coding Style Guide

https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md

PSR-3LOGGER INTERFACE

Logger Interface◦ The LoggerInterface exposes eight methods to write logs to the eight RFC 5424 levels (debug, info,

notice, warning, error, critical, alert, emergency)

◦ A ninth method, log, accepts a log level as first argument. Calling this method with one of the log level constants MUST have the same result as calling the level-specific method. Calling this method with a level not defined by this specification MUST throw a Psr\Log\InvalidArgumentException if the implementation does not know about the level. Users SHOULD NOT use a custom level without knowing for sure the current implementation supports it.

https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md

PSR-4 (PSR-0 extension)IMPROVED AUTOLOADING

PSR-0 way

PSR-4 way

Criticism

Often hearToo far

Subjective

Too specific

Not flexible

Become PHP-FIG community memberhttps://groups.google.com/forum/#!forum/php-fig

Thanks

• Email: [email protected]

• Skype: aliaksandr.simanovich

• Twitter: @simanjan

• Blog: simanovich.info