SitePoint Sponsor |
|
User Tag List
Results 1 to 10 of 10
Thread: The naming convention in PHP??
-
May 13, 2009, 18:13 #1
- Join Date
- Jul 2008
- Posts
- 220
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
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??
-
May 13, 2009, 18:24 #2
There is no official coding standards. Everyone has there own.
-
May 13, 2009, 18:32 #3
Yep, you are right!!!
and it's much easier for me too to read $var_name
-
May 13, 2009, 19:00 #4
- Join Date
- Jul 2006
- Location
- Augusta, Georgia, United States
- Posts
- 4,194
- Mentioned
- 17 Post(s)
- Tagged
- 5 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)
-
May 13, 2009, 21:15 #5
- Join Date
- Jul 2008
- Posts
- 220
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
-
May 13, 2009, 23:06 #6
- 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.
-
May 14, 2009, 06:47 #7
- 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();
-
May 14, 2009, 07:29 #8
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).
-
May 14, 2009, 07:51 #9
- 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.
-
May 14, 2009, 08:23 #10
- 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; }
}
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