PSR: Standards in PHP
Story
PHP Standards for Writing Code
PEAR
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-0
AUTOLOADER STANDARD
Tools
require() 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
◦ DoctrineCommonIsolatedClassLoader =>
/path/to/project/lib/vendor/Doctrine/Common/IsolatedClassLoader.php
◦ SymfonyCoreRequest => /path/to/project/lib/vendor/Symfony/Core/Request.php
◦ ZendAcl => /path/to/project/lib/vendor/Zend/Acl.php
◦ ZendMailMessage => /path/to/project/lib/vendor/Zend/Mail/Message.php
◦ namespacepackageClass_Name =>
/path/to/project/lib/vendor/namespace/package/Class/Name.php
◦ namespacepackage_nameClass_Name =>
/path/to/project/lib/vendor/namespace/package_name/Class/Name.php
PSR-0: not for PHP core
PSR-1
BASIC 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-2
CODING STYLE GUIDE
Coding Style Guide
https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md
PSR-3
LOGGER 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 PsrLogInvalidArgumentException 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 hear
Too far
Subjective
Too specific
Not flexible
Become PHP-FIG community member
https://groups.google.com/forum/#!forum/php-fig
Thanks
• Email: ax.simanovich@gmail.com
• Skype: aliaksandr.simanovich
• Twitter: @simanjan
• Blog: simanovich.info

PSR: Standards in PHP by Alex Simanovich

  • 1.
  • 2.
  • 3.
    PHP Standards forWriting Code PEAR Zend Framework Drupal Mine standards … etc
  • 4.
    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)
  • 5.
    FIG’s Goal The goalof the FIG is to create a dialogue between project representatives, with the aim of finding ways to work together (interoperability)
  • 6.
  • 7.
    Tools require() in PHP4 __autoload() in PHP 5 spl_autoload() and spl_autoload_register() in PHP 5.1.2
  • 8.
    Requirements ◦ A fully-qualifiednamespace 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.
  • 9.
    Example ◦ DoctrineCommonIsolatedClassLoader => /path/to/project/lib/vendor/Doctrine/Common/IsolatedClassLoader.php ◦SymfonyCoreRequest => /path/to/project/lib/vendor/Symfony/Core/Request.php ◦ ZendAcl => /path/to/project/lib/vendor/Zend/Acl.php ◦ ZendMailMessage => /path/to/project/lib/vendor/Zend/Mail/Message.php ◦ namespacepackageClass_Name => /path/to/project/lib/vendor/namespace/package/Class/Name.php ◦ namespacepackage_nameClass_Name => /path/to/project/lib/vendor/namespace/package_name/Class/Name.php
  • 10.
  • 11.
  • 12.
    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.
  • 13.
  • 14.
  • 15.
  • 16.
    Logger Interface ◦ TheLoggerInterface 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 PsrLogInvalidArgumentException 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
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
    Often hear Too far Subjective Toospecific Not flexible
  • 22.
    Become PHP-FIG communitymember https://groups.google.com/forum/#!forum/php-fig
  • 23.
    Thanks • Email: ax.simanovich@gmail.com •Skype: aliaksandr.simanovich • Twitter: @simanjan • Blog: simanovich.info

Editor's Notes

  • #2 http://net.tutsplus.com/tutorials/php/psr-huh/ https://groups.google.com/forum/#!forum/php-fig https://github.com/php-fig/fig-standards http://www.php-fig.org/ http://petermoulding.com/php/psr http://hybridlogic.co.uk/2012/04/psr-0-great-idea-bastardised-execution/ http://www.reddit.com/r/PHP/comments/1kvr1f/the_new_autoloading_standard_psr4_passes_entrance/ http://blog.ircmaxell.com/2011/11/on-psr-0-being-included-in-phps-core.html https://wiki.php.net/rfc/splclassloader/vote https://drupal.org/project/psr0 http://paul-m-jones.com/archives/2420
  • #5 PHP Standards Group Not only frameworks
  • #8 No matter how good the autoloading functionality is, it does not define how to implement it with existing codebases. For example, library X might approach the directory and classname structure differently than library Y, but you might want to use both! Writing a proper autoloader that knows where to look for all possible fully-qualified names, as well as test all file extensions (.class.php, inc.php, .php etc) will quickly become a mess.
  • #11 https://wiki.php.net/rfc/splclassloader https://wiki.php.net/rfc/splclassloader/vote
  • #18 https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md http://www.sitepoint.com/battle-autoloaders-psr-0-vs-psr-4/