TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Your Naming Conventions (http://www.talkphp.com/advanced-php-programming/4906-your-naming-conventions.html)

eStrategy 08-27-2009 04:42 PM

Your Naming Conventions
 
Please share with me you class, function and variable naming conventions.

Do you use m_ for member functions?
Propercase, lowercare etc..

Do you put a type identifier at the start of your variables?

$sString, $aArray etc..


What conventions do you use and why??

Thanks

adamdecaf 08-27-2009 10:49 PM

PHP Code:

define("SOME_CONSTANT"42);

class 
something_class // I always have "_class" after.
   
public var $foo_bar;

   public function 
this_function($var1$var2) {
       echo 
$var2 ' ' $var1;
   }



Enfernikus 08-27-2009 11:04 PM

Hungarian ( Which is CamelCase but with the datatype ) & CamelCase

cachepl0x 09-25-2009 11:24 AM

PHP Code:

# Comment with a pound

class Conf
{
    const 
name "name";
    const 
age  "20";
}

class 
Main extends Conf                    # Always capitalize class names.
{
    public 
$var      name;             # Typical
    
public $othervar age;              # No camelCase. I can't stand it.
    
    
public function FunctionName()         # Also Caps.
    
{
        echo 
$var "is " $othervar;
    }
    



Sirupsen 09-25-2009 12:13 PM

Classes: CamelCase (But their in 99/100 cases one word)
Functions: CamelCase
Strings: small_letters_with_underscores
Constants: CAPS

ETbyrne 09-26-2009 10:20 PM

Variable, function, and class names: lowercase with underscores
Constants: caps
Indention: tabs (doesn't work in this code editor)

PHP Code:

define('MY_CONSTANT','value');


class 
mysql
{
    private 
$con;


    public function 
__construct($host,$database,$user,$pass=NULL)
    {
        
// ...
    
}


    public function 
query($sql)
    {
        
// ...
        
return($results);
    }
}


$mysql = new mysql('localhost','mydb','root','pass');

$results $mysql->query('SELECT * FROM `mytable`'); 

I tend to avoid CamelCase because a lot of times you come across strange looking names like getId(). Then you have to decide between breaking your code convention to make it look right or just leaving it to look odd.

Plus typing all lowercase is easier on the fingers :-)

TheOnly92 09-27-2009 02:03 AM

All these are almost like religions, just find 1 that will suit for you and it will do good in your programming.

adamdecaf 09-27-2009 04:36 PM

Quote:

Originally Posted by TheOnly92 (Post 28538)
All these are almost like religions, just find 1 that will suit for you and it will do good in your programming.

"A good programmer would be able to switch styles from project to project."

Some programmers do hold their styles to similar statuses as religious beliefs.

ioan1k 09-28-2009 12:39 AM

CamelCasing everything except for array keys there are seperated with a "_"

I also keep a very tidy codestructure ...

I do follow this religiously ... as most experienced programmers do, as far as switching when I am on another project ... I either will integrate my standard in as conventions most I see are extremely sloppy or I lead the project and I set it anyway :-D

Quote:

I tend to avoid CamelCase because a lot of times you come across strange looking names like getId(). Then you have to decide between breaking your code convention to make it look right or just leaving it to look odd.
I too once followed the same mentality and used just about the same convention using underscores, but once you get used to CamelCasing you come to wonder why you coded that way in the first place, as i find it much much easier to read it CamelCased ... and the look doesnt have to make sense :)

php Code:
/**
     * Administration Login
     */

    public function login()
    {
        $this->view->headerTitle(__('Login'));
       
        if (!$this->session->sessionExists('admin_login_lock')) {
            $this->session->startSession('admin_login_lock');
        }

        if ($this->session->sessionExists('admin_login_lockout')) {
            $this->lock_out = true;
            $this->session->sessionDestroy('admin_login_lock');
            echo $this->view->getHtml('login');
        }

        if (!$this->request->isPost()) {
            $this->form = new Unus_Form('login_form', '');
            $username = $this->form->addElement('text', 'username');
            $username->setLabel(__('Username'))->setRequire(array(
                                                                  'msg' => array(
                                                                                 'required'     => __('Please enter your username'),
                                                                                 'minlength'    => __('Your username must be at least 4 characters'),
                                                                                 'alpha_num'    => __('I\m sorry, you are only allowed letters and numbers, please choose another')    ,
                                                                                ),
                                                                  'val' => array(
                                                                                 'required'     => 'true',
                                                                                 'minlength'    => '4',
                                                                                 'alpha_num'    => 'true',
                                                                                )
                                                                )
                                                            );
            $password = $this->form->addElement('password', 'password')->setRequire(array(
                                                                                          'msg' => array(
                                                                                                         'required' => __('Please enter you password'),
                                                                                                         //'pwd_chk' => __('Your password must must be at least 6 characters in length, contain a number, uppercase and lowercase letter') 
                                                                                                         ),
                                                                                          'val' => array(
                                                                                                         'required' => 'true',
                                                                                                         'pwd_chk' => 'true'
                                                                                                        )
                                                                                         )
                                                                                   );
            $submit = $this->form->addElement('submit', 'submit');
            $submit->setClass('key')->setValue(__('Login'))->setLabel(false);
            $button = $this->form->addElement('button', 'forgot_password');
            $button->setClass('help')->setValue(__('Lost Password'))->setLabel(false);

            $this->form->setDecorator(array(
               'form_start' => false,
               'form_end' => false,
               'row_start' => false,
               'row_end' => false,
               'label_start' => false,
               'label_end' => false,
               'element_start' => false,
               'element_end' => false,
               'element_submit_label_start' => false,
               'element_submit_label_end' => false,
               'element_submit_start' => '<div class="clear"></div>',
               'element_submit_end' => ' ',
            ));

            echo $this->view->getHtml('login');

        } else {

ETbyrne 09-28-2009 01:12 AM

Quote:

I do follow this religiously
I'm really hoping that's an overstatement... :-)

Quote:

I too once followed the same mentality and used just about the same convention using underscores, but once you get used to CamelCasing you come to wonder why you coded that way in the first place, as i find it much much easier to read it CamelCased ... and the look doesnt have to make sense :)
It just comes down to personal preference. Honestly it doesn't matter which one you use, as long as it is easy for you and other programmers to follow.

ioan1k 09-28-2009 01:30 AM

Quote:

Originally Posted by ETbyrne (Post 28569)
I'm really hoping that's an overstatement... :-)

Haha it was not :)

Standards to me are extremely important and 99% of the time will make or break wither or not I will even use code someone else has written.

A little off-topic but this is the reason that I like Python as these standards are enforced by the language...something I would like to see happen in PHP because you can't even read some php people have written :-P

Village Idiot 09-30-2009 04:47 PM

It depends on the language I use. When I do vb at work I use camel case, but when I do C at home I tend to use underscores. I always used underscores when coding in PHP.


All times are GMT. The time now is 12:27 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0