1.2.888. Order Of Declaration

The order used to declare members and methods has a great impact on readability and maintenance. However, practices varies greatly. As usual, being consistent is the most important and useful.

The suggested order is the following : traits, constants, properties, methods. Optional characteristics, like final, static… are not specified. Special methods names are not specified.

<?php

class x {
    use traits;

    const CONSTANTS = 1;
    const CONSTANTS2 = 1;
    const CONSTANTS3 = 1;

    private $property = 2;
    private $property2 = 2;
    private $property3 = 2;

    public function foo() {}
    public function foo2() {}
    public function foo3() {}
    public function foo4() {}
}

?>

1.2.888.1. Suggestions

  • Always declare class elements (traits, constants, properties, methods) in the same order.

1.2.888.2. Specs

Short name

Classes/OrderOfDeclaration

Rulesets

All, Coding conventions

Exakat since

0.11.7

PHP Version

All

Severity

Time To Fix

Precision

Medium

Features

class, anonymous-class, abstract

Available in

Entreprise Edition, Exakat Cloud