PSR-1_ Basic Coding Standard - PHP-FIG

Embed Size (px)

Citation preview

  • 7/25/2019 PSR-1_Basic Coding Standard - PHP-FIG

    1/6

    PHP-FIGHomeRecommendations (PSRs)MembersBylawsFAQsGet InvolvedPSR-1: Basic Coding StandardPSR-1: Basic Coding Standard

    Thissection of the standardcompriseswhat shouldbe consideredthe

    standardcoding elementsthat are requiredto ensure a high level of technical

    interoperability between sharedPHP code.

    The key words"MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",

    "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and"OPTIONAL" in

    thisdocument are to be interpretedasdescribedin RFC 2119.

    1. Overview

    FilesMUST use only

  • 7/25/2019 PSR-1_Basic Coding Standard - PHP-FIG

    2/6

    2. Files

    2.1. PHP Tags

    PHP code MUST use the long tagsorthe short-echo tags; it

    MUST NOT use the othertag variations.

    2.2. CharacterEncoding

    PHP code MUST use only UTF-8without BOM.

    2.3. Side Effects

    A file SHOULD declare newsymbols(classes, functions, constants, etc.) and

    cause no otherside effects, orit SHOULD execute logic with side effects, but

    SHOULD NOT do both.

    The phrase "side effects" meansexecution of logic not directly relatedto

    declaring classes, functions, constants, etc., merely f

    rom incl

    uding the file.

    "Side effects" include but are not limitedto: generating output, explicit use of

    require or include , connecting to external services, modifying ini settings,

    emitting errorsorexceptions, modifying global orstatic variables, reading

    from orwriting to a file, andso on.

    The following isan example of a file with both declarationsandside effects;i.e, an example of what to avoid:

    php

    // side effect: change ini settings

    ini_set('error_reporting', E_ALL);

    1: Basic Coding Standard - PHP-FIG http://www.php-fig.org/p

    6 19/06/2016 0

  • 7/25/2019 PSR-1_Basic Coding Standard - PHP-FIG

    3/6

    // side effect: loads a file

    include"file.php";

    // side effect: generates output

    echo"\n";

    // declaration

    functionfoo()

    {

    // function body

    }

    The following example isof a file that containsdeclarationswithout side

    effects; i.e., an example of what to emulate:

    php

    // declaration

    functionfoo()

    {

    // function body

    }

    // conditional declaration is *not* a side effect

    if(!function_exists('bar')) {

    functionbar()

    {

    // function body

    }

    }

    3. Namespace andClassNames

    NamespacesandclassesMUST followan "autoloading" PSR: [PSR-0, PSR-4].

    1: Basic Coding Standard - PHP-FIG http://www.php-fig.org/p

    6 19/06/2016 0

  • 7/25/2019 PSR-1_Basic Coding Standard - PHP-FIG

    4/6

    Thismeanseach classisin a file by itself, andisin a namespace of at least

    one level: a top-level vendorname.

    ClassnamesMUST be declaredin StudlyCaps .

    Code written forPHP 5.3andafterMUST use formal namespaces.

    Forexample:

    php

    // PHP 5.3 and later:

    namespaceVendor\Model;

    classFoo

    {

    }

    Code written for5.2.x andbefore SHOULD use the pseudo-namespacing

    convention of Vendor_ prefixeson classnames.

    php

    // PHP 5.2.x and earlier:

    classVendor_Model_Foo

    {

    }

    4. ClassConstants, Properties, andMethods

    The term "class" refersto all classes, interfaces, andtraits.

    4.1. Constants

    1: Basic Coding Standard - PHP-FIG http://www.php-fig.org/p

    6 19/06/2016 0

  • 7/25/2019 PSR-1_Basic Coding Standard - PHP-FIG

    5/6

    ClassconstantsMUST be declaredin all uppercase with underscore

    separators. Forexample:

    php

    namespaceVendor\Model;

    classFoo

    {

    constVERSION='1.0';

    constDATE_APPROVED='2012-06-01';

    }

    4.2. Properties

    Thisguide intentionally avoidsany recommendation regarding the use of

    $StudlyCaps , $camelCase , or $under_score property names.

    Whatevernaming convention isusedSHOULD be appliedconsistently within

    a reasonable scope. That scope may be vendor-level, package-level, class-

    level, ormethod-level.

    4.3. Methods

    MethodnamesMUST be declaredin camelCase() .

    Available tran

    slation

    s:

    English (official)

    1: Basic Coding Standard - PHP-FIG http://www.php-fig.org/p

    6 19/06/2016 0

  • 7/25/2019 PSR-1_Basic Coding Standard - PHP-FIG

    6/6

    Followuson Twitter Chat on IRC Channel Contribute via Github Join ourmailing list

    2016PHP Framework Interop Group. Site design by Jonathan Reinink.

    1: Basic Coding Standard - PHP-FIG http://www.php-fig.org/p