php psr standard 2014 01-22

16
PSR Introduction PHP for Enterprise

Upload: vo-duy-tuan

Post on 18-May-2015

3.655 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Php psr standard 2014 01-22

PSR IntroductionPHP for Enterprise

Page 2: Php psr standard 2014 01-22

Võ Duy TuấnCEO/Founder @ spiral.vn

PHP 5 Zend Certified Engineer

Mobile App Developer

Web Developer & Designer

Interest: o PHP

o Large System & Data Mining

o Web Performance Optimization

o Mobile Development

Page 3: Php psr standard 2014 01-22

Why Standard?

How case-sensitive server kills PSR-0?

Question & Answer

AGENDA

Page 4: Php psr standard 2014 01-22

1. Why Standard?

Page 5: Php psr standard 2014 01-22

PSR FAMILY (http://www.php-fig.org/)

• PSR-0• PSR-1• PSR-2• PSR-3• PSR-4

Page 6: Php psr standard 2014 01-22

PSR-0 : Autoloading

• 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 7: Php psr standard 2014 01-22

PSR-1 : Basic Coding Standard

• Files MUST use only <?php and <?= tags.• Files MUST use only UTF-8 without BOM for PHP code.• Files SHOULD either declare symbols (classes, functions,

constants, etc.) or cause side-effects (e.g. generate output, change .ini settings, etc.) but SHOULD NOT do both.

• Namespaces and classes MUST follow PSR-0.• Class names MUST be declared in StudlyCaps.• Class constants MUST be declared in all upper case with

underscore separators.• Method names MUST be declared in camelCase.

Page 8: Php psr standard 2014 01-22

PSR-2 : Coding Style Guide

• Code MUST follow PSR-1.• Code MUST use 4 spaces for indenting, not tabs.• There MUST NOT be a hard limit on line length; the soft limit MUST be 120 characters; lines

SHOULD be 80 characters or less.• There MUST be one blank line after the namespace declaration, and there MUST be one

blank line after the block of use declarations.• Opening braces for classes MUST go on the next line, and closing braces MUST go on the next

line after the body.• Opening braces for methods MUST go on the next line, and closing braces MUST go on the

next line after the body.• Visibility MUST be declared on all properties and methods; abstract and final MUST be

declared before the visibility; static MUST be declared after the visibility.• Control structure keywords MUST have one space after them; method and function calls

MUST NOT.• Opening braces for control structures MUST go on the same line, and closing braces MUST go

on the next line after the body.• Opening parentheses for control structures MUST NOT have a space after them, and closing

parentheses for control structures MUST NOT have a space before.

Page 9: Php psr standard 2014 01-22

2. How Case-sensitive server kills PSR-0 ?

Page 10: Php psr standard 2014 01-22

PSR-0 PROBLEM

• If user type: http://abc.com/admin/product-> Most framework rewrite engine will parsing URL and return information about included controller:

- Module: admin- Controller: product- Action: index (default)

Page 11: Php psr standard 2014 01-22

PSR-0 PROBLEM (cont..)

• If follow PSR-0, your autoloader will include controller file somewhere like:

…/controller/admin/product.php

• And, your controller class must be something like :

<?php

namespace controller\admin;

class product extends basecontroller{

…}

Page 12: Php psr standard 2014 01-22

Where is Problem ?!

Page 13: Php psr standard 2014 01-22

PSR-0 PROBLEM (cont..)

• If follow PSR-1, your Class name must be something like:Product not product

• And, if your classname is Product, your controller file must be:…/controller/admin/Product.php

• So that, for autoloader works, your URL must be:http://abc.com/admin/Product

• So, you MUST change all URL structure of your website

Page 14: Php psr standard 2014 01-22

Solution…

Page 15: Php psr standard 2014 01-22

CLASSMAP

• Hook before autoloader include controller file, something like this:

…'controller\admin\product' => 'Controller/Admin/Product.php'

Page 16: Php psr standard 2014 01-22

Thank you!

CONTACT ME:

[email protected]

0938 916 902

http://bloghoctap.com/