PHP conforms to the PSR Programming specification example

Source: Internet
Author: User
Tags php framework
Good code writing habits can make people pleasing to the eye, the following this article to share the code of PHP to meet the PSR programming specifications, the need for friends can refer to, let's take a look at it.

Objective

About the development of the standard this piece, can say has always been different styles, each home has a play, folk is also a personal play. At present, our domestic more well-known frameworks (Yii,laravel) have supported composer and joined the Php-fig (PHP Framework program group).

The automatic loading of composer supports php-fig specified PSR-0 and PSR-4 specifications to implement the automatic loading mechanism, and composer recommended PSR-4

Php-fig

This is a voluntary informal body, but in terms of the current impact on us, it may have been the default for a public trust organization, indeed has made a lot of very good norms

At present, from the official website, has voted (http://www.php-fig.org/psr/) passed the 7 large specifications

    1. PSR-0 Automatic loading specification (officially obsolete, mostly php5.3 without namespace previously)

    2. PSR-1 Coding Specification

    3. PSR-2 Coding Style Recommended

    4. PSR-3 Log Interface

    5. PSR-4 improved automatic loading specification (official recommendation, the specification is more concise conditioning clear)

    6. PSR-6 Cache Interface

    7. PSR-7 HTTP Message interface

Instance

<?phpnamespace Standard; Top namespace//empty line use Test\testclass;//use introduces class/** * Class Description * * Class name must be capitalized at the beginning of the hump.  */abstract class Standardexample//{} must be wrapped with a newline {/** * constant description. * * @var String */const THIS_IS_A_CONST = ';  Constant all uppercase underline split/** * attribute description. * * @var String */public $nameTest = ';  The property name suggests that the lowercase hump//member property must be added public (cannot be omitted), private, protected modifier/** * Property Description. * * @var String */private $_privatenametest = ';  Class Private member property, "personal advice" underline lowercase start Hump/** * constructor. * * Constructor Description * * @param string $value parameter name/description */Public function __construct ($value = ")//Member method must add public (cannot be omitted), privat  E, protected modifier {//{} must be wrapped $this->nametest = new TestClass ();  Chained Operation $this->nametest->functionone ()->functiontwo ()->functionthree (); A piece of code logic to complete the line break/\ Code ...}  /** * Member method name. * * Member Method Description * * @param string $value parameter name/description * * @return return value type return value Description * Return value type: string,array,object,mixed (multiple, indeterminate), VO ID (no return value) */Public Function testfunction ($value = ')//member method must start with a lowercase hump {//Code ...}  /** * Member method name. * * Member Method Description * * @param string $value parameter name/description * * @return return value type return value Description */Private Function _privatetestfunction ($val  UE = ')//Private Member method "personal suggestion" underline lowercase start Hump {//code ...}/** * Member method name. * * Member Method Description * * @param string $value parameter name/description * * @return return value type return value Description */public static function Staticfunction ($val  UE = ')//static after modifier {//code ...}/** * Member method name. * * Member Method Description * * @param string $value parameter name/description * * @return return value type return value Description */abstract public Function abstractfunction ( $value = ");  Abstract/** * Member method name before modifier.  * * Member Method Description * * @param string $value parameter name/description * * @return return value type return value Description */FINAL public function finalfunction ($value  = ')//final located before modifier {//code ...}/** * Member method name.  * * Member Method Description * * @param string $valueOne parameter name/description * @param string $VALUETWO parameter name/description * @param string $valueThree parameter name/description * @param string $valueFour parameter name/description * @param string $valueFive parameter name/description * @param string $VALUESIX parameter name/description * * @return return Return value type returns a value description */Public Function toolangfunction ($valueOne = ',//variable name can be named after the first hump or underline, personal that habit, it is said that the underline readability good $valueTwo = ", $valueThree = ', $valueFour = ', $valueFive = ', $valueSix = ')//parameter too many newline {if ($valueOne = = = $valueTwo) {//Control structure plus space, same as {one  Line, (right Bien Hoa) left without space//code ...}   Switch ($valueThree) {case ' value '://code ... break;  Default://code ... break;  } do {//code ...} while ($valueFour <= 10);  while ($valueFive <=) {//code ...} for ($i =0; $i < $valueSix; $i + +) {//Code ...}}}

The above is the whole content of this article, I hope that everyone's study has helped.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.