Top Banner
PSR: Standards in PHP
23
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: PSR: Standards in PHP by Alex Simanovich

PSR: Standards in PHP

Page 2: PSR: Standards in PHP by Alex Simanovich

Story

Page 3: PSR: Standards in PHP by Alex Simanovich

PHP Standards for Writing CodePEAR

Zend Framework

Drupal

Mine standards

… etc

Page 4: PSR: Standards in PHP by Alex Simanovich

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)

Page 5: PSR: Standards in PHP by Alex Simanovich

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)

Page 6: PSR: Standards in PHP by Alex Simanovich

PSR-0AUTOLOADER STANDARD

Page 7: PSR: Standards in PHP by Alex Simanovich

Toolsrequire() in PHP 4

__autoload() in PHP 5

spl_autoload() and spl_autoload_register() in PHP 5.1.2

Page 8: PSR: Standards in PHP by Alex Simanovich

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.

Page 9: PSR: Standards in PHP by Alex Simanovich

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

Page 10: PSR: Standards in PHP by Alex Simanovich

PSR-0: not for PHP core

Page 11: PSR: Standards in PHP by Alex Simanovich

PSR-1BASIC CODING STANDARD

Page 12: PSR: Standards in PHP by Alex Simanovich

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.

Page 13: PSR: Standards in PHP by Alex Simanovich

PSR-2CODING STYLE GUIDE

Page 14: PSR: Standards in PHP by Alex Simanovich

Coding Style Guide

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

Page 15: PSR: Standards in PHP by Alex Simanovich

PSR-3LOGGER INTERFACE

Page 16: PSR: Standards in PHP by Alex Simanovich

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

Page 17: PSR: Standards in PHP by Alex Simanovich

PSR-4 (PSR-0 extension)IMPROVED AUTOLOADING

Page 18: PSR: Standards in PHP by Alex Simanovich

PSR-0 way

Page 19: PSR: Standards in PHP by Alex Simanovich

PSR-4 way

Page 20: PSR: Standards in PHP by Alex Simanovich

Criticism

Page 21: PSR: Standards in PHP by Alex Simanovich

Often hearToo far

Subjective

Too specific

Not flexible

Page 22: PSR: Standards in PHP by Alex Simanovich

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

Page 23: PSR: Standards in PHP by Alex Simanovich

Thanks

• Email: [email protected]

• Skype: aliaksandr.simanovich

• Twitter: @simanjan

• Blog: simanovich.info