Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Core framework code style

Alexander Makarov edited this page · 22 revisions

The following code style is used for Yii 1.x development. If you want to pull-request code into the core, consider using it. We aren't forcing you to use this code style for your application. Feel free to choose what suits you better.

There is a CodeSniffer ruleset for this standard developed by @Ardem: https://github.com/Ardem/yii-coding-standard

1. Overview

  • Files MUST use only <?php tags.
  • Files MUST use only UTF-8 without BOM for PHP code.
  • Code MUST use tabs for indenting, not spaces.
  • 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.
  • Property names MUST be declared in camelCase.
  • Property names MUST start with an initial underscore if they are private.
  • Indentation uses Allman style with single line expressions on next line, without braces.
  • Always elseif instead of else if.

2. Files

2.1. PHP Tags

PHP code MUST use the long <?php ?> tags; it MUST NOT use the other tag variations.

2.2. Character Encoding

PHP code MUST use only UTF-8 without BOM.

3. Class Names

Class names MUST be declared in StudlyCaps. Core classes should be prefixed with C. For example, CController, CWidget. Application and extension classes should not use C as prefix. For extensions it's common to use E.

4. Classes

The term "class" refers to all classes and interfaces here.

4.1. Constants

Class constants MUST be declared in all upper case with underscore separators. For example:

<?php
class Foo
{
    const VERSION='1.0';
    const DATE_APPROVED='2012-06-01';
}

4.2. Properties

Property names MUST be declared in camelCase. Property names MUST start with an initial underscore if they are private.

For example:

<?php
class Foo
{
    public $publicProp;
    protected $protectedProp;
    private $_privateProp;
}

4.3. Methods

Method names MUST be declared in camelCase().

4.4 Doc blocks

@param, @var, @property and @return must declare types as boolean, integer, string, array or null. You can use a class names as well such as CModel or CActiveRecord. For a typed arrays use ClassName[].

4.5 Constructors

  • __construct should be used instead of PHP 4 style constructors.
  • When instantiating class it should be new MyClass(); instead of new MyClass;.
Something went wrong with that request. Please try again.