Naming Conventions

Global Variables and Functions

If your package needs to define global variables, their names should start with a single underscore followed by the package name and another underscore. For example, the PEAR package uses a global variable called $_PEAR_destructor_object_list.

Global functions should be named using the "studly caps" style (also referred to as "bumpy case" or "camel caps"). In addition, they should have the package name as a prefix, to avoid name collisions between packages. The initial letter of the name (after the prefix) is lowercase, and each letter that starts a new "word" is capitalized. An example:

XML_RPC_serializeData()

Classes

Classes should be given descriptive names. Avoid using abbreviations where possible. Class names should always begin with an uppercase letter. The PEAR class hierarchy is also reflected in the class name, each level of the hierarchy separated with a single underscore. Examples of good class names are:

Log Net_Finger HTML_Upload_Error

Class Variables and Methods

Class variables (a.k.a properties) and methods should be named using the "studly caps" style (also referred to as "bumpy case" or "camel caps"). Some examples (these would be "public" members):

$counter connect() getData() buildSomeWidget()

Private class members are preceded by a single underscore. For example:

$_status _sort() _initTree()

The following applies to PHP5.

Protected class members are not preceded by a single underscore. For example:

protected $somevar protected function initTree()

Constants

Constants should always be all-uppercase, with underscores to separate words. Prefix constant names with the uppercased name of the class/package they are used in. Some examples:

DB_DATASOURCENAME SERVICES_AMAZON_S3_LICENSEKEY
The true, false and null constants are excepted from the all-uppercase rule, and must always be lowercase.
Example URLs (Previous) File Formats (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

Note by: user@example.com
codextasy, I've read some PEAR packages code and they use "public $foo_bar" and "private $_foo_bar".
Note by: codextasy
What about local variables inside functions and methods:
$my_local_var or $myLocalVar?
Note by: Carl
Yes, PHP 5.x has truly private methods and properties if they are preceded by the "private" keyword.
Note by: hablutzel1@hotmail.com
I supposed that in php5 private properties are really private. or is it wrong?
Note by: user@example.com
PHP does not yet support truly-enforceable private namespaces

In PHP 5 you have truly-enforceable private namespaces, don't you?