SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    220
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question The naming convention in PHP??

    i'd searched many hours but couldn't find an official recommendation for naming PHP variables.

    i think the official documents in PHP always name the variables with underscore like $var_name.

    then there are many PHP source codes by others especially those from Java camp name their variables with camel style like $varName.

    i find that underscore $var_name is easier to read and find, but how about you??

    or am i missing the official PHP variable naming convention??

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    There is no official coding standards. Everyone has there own.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Enthusiast wihee's Avatar
    Join Date
    Nov 2008
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep, you are right!!!
    and it's much easier for me too to read $var_name

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,146
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    I prefer:

    • CamelCase for classes
    • modified camelCase for variables and properties(static and dynamic).
    • modified camelCase for functions and methods (underscore prefix if private or protected)
    • underscore lowercase for model property names
    • underscore for files names (file.class.php or file.interface.php for classes and interfaces specifically)

  5. #5
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    220
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    There is no official coding standards. Everyone has there own.
    perhaps we should suggest one to Zend

  6. #6
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's the PEAR conventions. I follow those and a lot of the newer PHP classes seem to follow them.

    I guess you could say that it's becoming de facto. Zend Framework's conventions are based off PEAR too, so it's slowly spreading.

  7. #7
    Grumpy Minimalist
    Join Date
    Jul 2006
    Location
    Ontario, Canada
    Posts
    424
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I prefer:

    • CamelCase for classes
    • UPPERCASE_AND_UNDERSCORES for constants
    • modified camelCase for variables and properties
    • modified camelCase for functions and methods
    • Accessors and mutators also use modified camelCase: getProperty(), setProperty(value)
    • modified camelCase with an underscore prefix for protected properties (I don't use private, and this underscore prefix has significance in my magic __get and __set methods)
    • Class_Names_Have_A_Hierarchical_Structure and are reflected in the directory paths to the files. All files and directories are Capitalized, and don't need separators. All files end simply in .php, even if they contain classes or interfaces. Interfaces have the final name in the hierarchy prefixed with an I.
    • Procedural php files are not capitalized, e.g. scaffolding.php
    • Looping variables are $i, $j, and $k (if I need more I start to question what I'm doing), unless there is something more appropriate in the context such as $x, $y, $z
    • Stored upper bounds for loops are $n, $m, and $c, unless there is something more appropriate such as $cols, $rows, etc.


    PHP Code:
    //Stored in: lib/Core/Syntax/Demonstration.php
    class Core_Syntax_Demonstration {
        const 
    CLASS_CONSTANT 0;
        protected 
    $_protectedVar;
        public 
    $publicProperty//Although, I never use public properties

        
    public function __construct() {}

        public function 
    getGetter() {}
        public function 
    setSetter($value) {}

        public function 
    causeMeow() {}

        protected function 
    protectedMethod($arr) {
            
    $n count($arr);
            for (
    $i 0$i $n; ++$i) {
                
    //Do something
            
    }
        }
    }

    //Stored in: procedural.php
    $syntax = new Core_Syntax_Demonstration(); 
    I use these conventions in most of the other languages that I know, where applicable. The only reason I ever change them is if I'm expanding on a project started by someone else, in which case I'll mimic their conventions.

  8. #8
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    The examples in the PHP manual have to adhere to the PEAR Coding Standards, that's as close to official as you're going to get. There's also a Coding Standard for the Zend Framework (and others for most other frameworks).
    Salathe
    Software Developer and PHP Manual Author.

  9. #9
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I try to use Zend Framework coding standards but from time to time I still manage not to adhere and use some weird naming convention of mine.

  10. #10
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I camelcase pretty much everything. I see no real need to change how you name something simply because of what it is.

    For example, I write my functions like FunctionName(), I write my variables like $VariableName, my classes like ClassName and my class methods/properties the same.

    The only exception is in an object's Set methods, where I modify the camelcase to a lowercase starting letter, i.e.:
    PHP Code:
    <?php
    Class Person{
        Protected 
    $Name$DateOfBirth;
        Public Function 
    GetName(){ return $this->Name; }
        Public Function 
    GetDateOfBirth(){ return $this->DateOfBirth; }
        Public Function 
    SetName($name){ $this->Name $name; }
        Public Function 
    SetDateOfBirth($dateOfBirth){ $this->DateOfBirth $dateOfBirth; }
    }
    As you can see, I also put simple get/set functions with no extra functionality on a single line.

    All of this stems from other programming languages. For example, in C++:
    Code c++:
    Class Person{
        Protected:
            char *Name;
            Date *DateOfBirth; //Date being a user-defined class
        Public:
            char *GetName(){ return Name; }
            Date *GetDateOfBirth(){ return DateOfBirth; }
            void SetName(Char *name){ Name = name; }
            void SetDateOfBirth(Date *dateOfBirth){ DateOfBirth = dateOfBirth; }
            int GetAge();
    }

    Because I detest the use of having to use '$this' or, in most languages 'this', I alter the parameter name slightly so that there is a noticable difference between the member and the parameter.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •