PSR-1: Basic Coding Standard
Definitions of key words (Must, Must Not, Required, Shall, Shall Not, Should, Should Not, Recommended, May, Optional) - RFC 2119
1. Files
PHP Tags
Files MUST use only <?php and <?= tags.
( <? conflicts with <?xml, <?= called short echo tag and it's very controversial )
Character Encoding
File MUST use only character encoding UTF-8 without BOM for PHP code - Setting on php.ini
Side Effects
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.
The phrase “side effects” means execution of logic not directly related to declaring classes, functions, constants, etc., merely from including the file.
“Side effects” include but are not limited to: generating output, explicit use of require or include, connecting to external services, modifying ini settings, emitting errors or exceptions, modifying global or static variables, reading from or writing to a file, and so on.
Bad Example:
<?php
// side effect: change ini settings
ini_set('error_reporting', E_ALL);
// side effect: loads a file
include "file.php";
// side effect: generates output
echo "<html>\n";
// declaration
function foo()
{
// function body
}
2. Namespace and Class Name
Namespaces and classes MUST follow an “autoloading” PSR-4
This means each class is in a file by itself, and is in a namespace of at least one level: a top-level vendor name.
Class names MUST be declared in StudlyCaps( also known as PascalCase).
SudlyCaps example - BackColor
Code written for PHP 5.3 and after MUST use formal namespaces.
<?php
// PHP 5.3 and later:
namespace Vendor\Model;
class Foo
{
}
3. Class Constants, Properties, and Methods
Constant
Class constants MUST be declared in all upper case with underscore separators. For example:
<?php
namespace Vendor\Model;
class Foo
{
const VERSION = '1.0';
const DATE_APPROVED = '2012-06-01';
}
Properties
This guide intentionally avoids any recommendation regarding the use of$StudlyCaps
,$camelCase
, or$under_score
property names.
Whatever naming convention is used SHOULD be applied consistently within a reasonable scope. That scope may be vendor-level, package-level, class-level, or method-level.
Methods
Method names MUST be declared incamelCase()
Whatever naming convention is used SHOULD be applied consistently within a reasonable scope. That scope may be vendor-level, package-level, class-level, or method-level.