Naming Conventions

Hi All,

Just a small survey on naming convention while you write your own PHP code (classes, functions, variables etc.). I do normally like this.


Class 						- MyClass
Class Object 					- $objMyClass
Class Member variable (public/protected) 	- $my_member_var
Class Member Variable (Private) 		- $_my_member_var
Class Member Function (public/protected) 	- myMemberFunction
Class Member Function (private) 		- _myMemberFunction
Normal PHP Variables 				- $my_var_name
Normal Function (outside class) 		- my_function_name
Constant (defined) 				- CONSTACT_NAME
Static Variable 				- $_STATIC_VARIABLE_NAME

Please you can add if anything is missed.

I know this does not make such meaning here, but I just want to know how you all guys do in what way.

Thankin you all in advance.

I use javaStyleSyntax for everything except CONSTANTS.

This is mine convention:

Class 						- MyClass
Class Object 					- $objMyClass
Class Member variable (public/protected) 	- $my_member_var
Class Member Variable (Private) 		- $_my_member_var
Class Member Function (public/protected) 	- myMemberFunction
Class Member Function (private) 		- _myMemberFunction
Normal PHP Variables 				- $my_var_name
Normal Function (outside class) 		- myFunctionName
Constant (defined) 				- CONSTACT_NAME
Static Variable 				- $same_as_normal_variable

Filenames:
Class Filename - myclass.class.php
Other Filename - other_filename.php

As a personal preference I hate underscores

$same_as_normal_variable

will be $SameAsNormalVariable -> similar to naming vars and mc’s in Action Script

Cheers

I try to follow this style myself:
The only time I really use underscores is for constants.


<?php namespace [Vendor]\\[Project]\\[Package];
/**
 * Short Description...
 *
 * Long Description can span many lines and contain examples or any other
 * details. Most cases this portion will be blank.
 *
 * @copyright 2009 [Author|Company]
 * @license   http://[vendor]/legal/oss/license BSD License
 * @license   http://[vendor]/legal/pro/license Proprietary License
 * @link      http://[project].[vendor]/api/[package]/[component]
 *
 * @category  [project]
 * @package   [package]
 * @version   $Id$
 */

# 80 char range
#-------------------------------------------------------------------------------

use Some\\OtherA\\Name\\Space as AliasA,
    Some\\OtherB\\Name\\Space as AliasB;

/**
 * Description and example of the inner workings of this compnent.
 *
 * @category [project]
 * @package [package]
 */
class ComponentCamelCase
{
    const
        CONST_ONE   = 1,
        CONST_TWO   = 2,
        CONST_THREE = 3;

    protected // Always protected!
        $varOne   = 'default',
        $varTwo   = 'default',
        $varThree = 'default';

    #-------------------------------------------------------------------------------

    # void ( string $a, mixed $b )
    # throw UnknownException, KnownException
    public function camelCase ( $a, $b )
    {
        #-------------------------------------------------------------------------------

        if ( expression ) {
            // multi-line action...
        } elseif ( expression ) {
            // more multi-line actions...
        } else // fallback action...

        // or, for single line
        if ( expression ) // action...
            // or put it under...

        $arr = array( 'key' => 'value' );
        $arr['key'] or $arr[ $key ];

        # mixed ( string $a, mixed $b ) + array $global
        # throw UnknownException, KnownException
        $call = function ( $a, $b ) use ( $global ) {
            // action...
        };

        return $this; // Always return $this unless it must return something else.
    }

    # void ()
    protected function specialMethod () {
        return $this;
    }
}


No internal PHPDoc? :confused:

Depends on who I’m writing the code for and at what stage. During the initial development and testing phases I use the small form for quick glances. Akin to the documentation on PHP.net.

Later on I add PHPDoc style documentation in the code once everything is nailed down. Otherwise I don’t feel like rewriting this verbose style every time I do some re-thinking of the design of the methods. (Which happens a lot…) :stuck_out_tongue: