Bài giảng Phát triển phần mềm nguồn mở - Bài 3: PHP Standards Recommendations - Nguyễn Hữu Thể

PHÁT TRIỂN PHẦN MỀM NGUỒN MỞ  
PHP Standards Recommendations  
Nguyễn Hữu Thể  
Index by Status (Accepted)  
Num Title  
Editor  
Coordinator Sponsor  
1 Basic Coding Standard Paul M. Jones N/A  
N/A  
N/A  
Paul M. Jones N/A  
Jordi  
N/A  
N/A  
Boggiano  
4 Autoloading Standard Paul M. Jones Phil Sturgeon Larry Garfield  
Paul  
Larry Garfield  
Robert Hafner  
Dragoonis  
Matthew  
Weier  
O'Phinney  
Beau  
Simensen  
7
Paul M. Jones  
Matthew  
Larry Garfield Weier  
O'Phinney  
Marc  
Alexander  
3
PSR-1: Basic Coding Standard  
1. Overview  
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 an "autoloading" PSR:  
[PSR-0, PSR-4].  
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.  
4
PSR-1: Basic Coding Standard  
SHOULD NOT  
SHOULD  
<?php  
<?php  
// declaration  
function foo()  
// side effect: change ini settings  
ini_set('error_reporting', E_ALL);  
{
// side effect: loads a file  
// function body  
}
include "file.php";  
// side effect: generates output  
echo "<html>\n";  
// conditional declaration is *not* a side  
effect  
if (! function_exists('bar')) {  
// declaration  
function foo()  
{
function bar()  
{
// function body  
// function body  
}
}
}
5
PSR-1: Basic Coding Standard  
Namespace and Class Names  
Namespaces and classes MUST follow an "autoloading" PSR:  
[PSR-0, PSR-4].  
Class names MUST be declared in StudlyCaps.  
// PHP 5.3 and later:  
namespace Vendor\Model;  
class Foo  
{
}
6
PSR-1: Basic Coding Standard  
Class Constants, Properties, and Methods  
The term "class" refers to all classes, interfaces, and traits.  
Class constants MUST be declared in all upper case with  
underscore separators.  
namespace Vendor\Model;  
class Foo  
{
const VERSION = '1.0';  
const DATE_APPROVED = '2012-06-01';  
}
7
PSR-1: Basic Coding Standard  
Properties  
• This guide intentionally avoids any recommendation  
regarding the use of $StudlyCaps, $camelCase, or  
$under_score property names.  
Methods  
• Method names MUST be declared in camelCase().  
8
PSR-2: Coding Style Guide  
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.  
9
PSR-2: Coding Style Guide  
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.  
10  
namespace Vendor\Package;  
use FooInterface;  
use BarClass as Bar;  
use OtherVendor\OtherPackage\BazClass;  
class Foo extends Bar implements FooInterface  
{
public function sampleMethod($a, $b = null)  
{
if ($a === $b) {  
bar();  
} elseif ($a > $b) {  
$foo->bar($arg1);  
} else {  
BazClass::bar($arg2, $arg3);  
}
}
final public static function bar()  
{
// method body  
}
}
PSR-3: 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 the first argument.  
Calling this method with one of the log level constants MUST  
have the same result as calling the level-specific method.  
Users SHOULD NOT use a custom level without knowing for  
sure the current implementation supports it.  
12  
PSR-4: Autoloader  
The term "class" refers to classes, interfaces, traits, and other  
similar structures.  
A fully qualified class name has the following form:  
\<NamespaceName>(\<SubNamespaceNames>)*\<ClassName>  
The fully qualified class name MUST have a top-level  
namespace name, also known as a "vendor namespace".  
The fully qualified class name MAY have one or more sub-  
namespace names.  
The fully qualified class name MUST have a terminating class  
name.  
13  
PSR-4: Autoloader  
Underscores have no special meaning in any portion of the  
fully qualified class name.  
Alphabetic characters in the fully qualified class name MAY  
be any combination of lower case and upper case.  
All class names MUST be referenced in a case-sensitive  
fashion.  
When loading a file that corresponds to a fully qualified class  
name ...  
14  
PSR-4: Autoloader  
The contiguous sub-namespace names after the "namespace  
prefix" correspond to a subdirectory within a "base directory",  
in which the namespace separators represent directory  
separators. The subdirectory name MUST match the case of  
the sub-namespace names.  
The terminating class name corresponds to a file name ending  
in .php. The file name MUST match the case of the  
terminating class name.  
Autoloader implementations MUST NOT throw exceptions,  
MUST NOT raise errors of any level, and SHOULD NOT  
return a value.  
15  
PSR-6: # Introduction  
The goal of this PSR is to allow developers to create  
cache-aware libraries that can be integrated into  
existing frameworks and systems without the need  
for custom development.  
16  
PSR-7: HTTP message interfaces  
This document describes common interfaces for  
representing HTTP messages as described in :  
• RFC 7230  
• RFC 7231,  
• URIs for use with HTTP messages as described in RFC  
3986.  
HTTP messages are the foundation of web  
development.  
• Web browsers and HTTP clients such as cURL create HTTP  
request messages that are sent to a web server, which  
provides an HTTP response message.  
• Server-side code receives an HTTP request message, and  
returns an HTTP response message.  
17  
pdf 17 trang yennguyen 22300 Free
Bạn đang xem tài liệu "Bài giảng Phát triển phần mềm nguồn mở - Bài 3: PHP Standards Recommendations - Nguyễn Hữu Thể", để tải tài liệu gốc về máy hãy click vào nút Download ở trên

File đính kèm:

  • pdfbai_giang_phat_trien_phan_mem_nguon_mo_bai_3_php_standards_r.pdf