Giter Club home page Giter Club logo

php_coding_standards's Introduction

PHP Coding Standards

This document describe about a professional standards on my own project using CakePHP. The standards is base on PSR-1 e CakePHP Coding Standards.

If you need to use, feel free.

  1. Directories and Files

  • To open php script tags, use <?php or <?= if you like to print the output.

  • Use only UTF-8 without BOM for PHP code, this make your code clean of charsets problems.

  • Do not use the same file with more than one Class.

  • 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.

  • For Class file not close the php tag ?>.

  • Directory names must be named in StudleCaps.

  • Document the variable, methods and classes.

  • Samples:

//filepath: Controller/PagesController.php
<?php
/**
 * This class is a sample class standards about the writing code in php.
 *
 * PHP 5
 *
 * Company Name: (http://www.companyname.com)
 * Copyright 2005-2012, Company Name (http://www.companyname.com)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2012, Company Name (http://www.companyname.com)
 * @link          http://www.companyname.com My Company link description
 * @package       app.Controller
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
class PagesController extends AppController{

    /**
    * @var String
    * Description of the constant
    */
    const VARIABLE_NAME = 'value';
    
    /**
    * @var String
    * Description of the variable
    */
    private $__variableName = 'value';
    
    /**
    * @var String
    * Description of the variable
    */
    protected $_variableName = 'value';
    
    /**
    * @var String
    * Description of the variable
    */
    public $variableName = 'value';
    
    /**
    * @var String
    * Description of the variable
    */
    public static $staticVar = 'value';
    
    /**
    * Function name
    * Description of the method behavior
    * @param $param1 String Description of the param1
    * @return void
    */
    public function index($param1 = null) {
        //some code here
        $this->__process();
    }
  
    /**
    * Function name
    * Description of the method behavior
    * @return array List of users
    */
    private function __process() {
        //some code here
        return array();
    }

}
  1. Best practices for structures. Conditions and loops.

  • Use 4 (four) spaces to ident, replace on your IDE the tab for four spaces.
class ClassName {
    public function nameMethod() {
        //method content
    }
}
  • Do not use the nested ternary structure.
  • Use ternary only for simple conditions.
  • The if, else structure must be writed using the bracket after the closing parenthesis and one space between the parenthesis and bracket.
  • For else if use on same line of the closing bracked of the if separeted by one space between the ending bracked and the else instruction.
  • For methods, functions and classes use the bracked after one space of the class name, or extended class, interface or traits.
//Wrong ternary
$result = $var == 1 ? 'output' : $var == 2 ? 'continue' : 'none';

//Right ternary
$isMale = ($var ==  'male') ? true : false;

//In the case above prefer to use if, else structure
$result = 'none';
if($var == 1) {
    $result = 'output';
} else if($var == 2) {
    $result = 'continue';
}
  • Function must be called without spaces between the function name and the parenthesis, the spaces is only used for separete function attributes.
$result = foo(1, 2, 3);
  • Variables names follow the write on camelCase, except for classes instances, where you use the same name of the Class.
$PagesController = new PagesController();
  • Nested methods use on method by line and ident the nexts methods by one tab (4 spaces)
$ComponentName->init()
    ->isPost()
    ->params(array('url' => 'http://www.michaelmafort.com.br'))
    ->execute();
  • Comments inline use //.
  • Comments multiline use /**/
//Coment inline
/*
Multiline comment
Describe your contents, todos, mistakes and jokes 
(I'm joking, of your write the jokes on your code, never write this :P)
*/
  1. Includes

If you are using CakePHP framework the includes are maked by the App static class, something like this:

//to import any file
//docs http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#including-files-with-app-import
App::import(mixed $type = null, string $name = null, mixed $parent = true, array $search = array(), string $file = null, boolean $return = false);

//to import cakephp objects
App::uses('PostsController', 'Controller');
App::uses('AuthComponent', 'Controller/Component');
App::uses('MyModel', 'Model');
App::uses('TreeBehavior', 'Model/Behavior');
App::uses('ThemeView', 'View');
App::uses('HtmlHelper', 'View/Helper');
App::uses('PaymentProcessor', 'Lib');
App::uses('Textile', 'Vendor');
App::uses('String', 'Utility');

php_coding_standards's People

Contributors

michaelmafort avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.