Search code examples
phpconstantsclass-constants

Code style defining multiple constants in PHP


Is there any significant difference in how I define multiple constants in PHP class between:

this:

<?php
abstract class Some_Class
{
    const CONSTANT_ONE   = 101;
    const CONSTANT_TWO   = 102;
    const CONSTANT_THREE = 103;
}

and probably closer to JS style of defining:

<?php
abstract class Some_Class
{
    const CONSTANT_ONE   = 101,
          CONSTANT_TWO   = 102,
          CONSTANT_THREE = 103
    ;
}

Solution

  • Regarding syntax there is no difference.

    Even the PHP-FIG Standards concerning constants allows the use of the second form documentation-wise (they call it compound statements).

    Personally I prefer the first form :)

    abstract class Some_Class {
        /**
         * @var int CONSTANT_ONE This constant is magical
         */
        const CONSTANT_ONE   = 101;
    
        /**
         * @var float CONSTANT_TWO This is Pi!
         */
        const CONSTANT_TWO   = 3.14;
    
        /**
         * @var string CONSTANT_THREE The magical door number
         */
        const CONSTANT_THREE = "103a";
    }