Jump to content

OOP convention.. how do you do it?


nrg_alpha

Recommended Posts

Upon seeing this thread: http://www.phpfreaks.com/forums/index.php/topic,282348.0.html (which I found interesting seeing other peoples' opinions on something so trivial..) I figured I would ask about how you set up certain things in OOP. I realise that there are some 'best practices' for organization / readability and whatnot. Despite this, I'm just curious more than anything else to see which methods you choose (ignoring curly brace formatting / spacing):

 

Question #1: When declaring properties, which format do you use?

// Example #1a:
class Foo{
    public $myVar01, $myVar02;
    ....
}

//Example #1b:
class Foo{
    public $myVar01;
    public $myVar02;
    ....
}

//Example #1c:
class Foo{
    var $myVar01; // You should really start using PHP 5
    ....
}

 

Question #2: With regards to say, property visibility naming conventions for example (as this can apply to methods as well), do you use variable name prefixing to depict its visibilty?

//Example #2a:
class Foo{
    public $make; // public, so no prefix
    private $_model; // private, so varaible contains initial underscore to denote this visibility
    protected $_year; // protected so variable also contains initial underscore to denote this visibility
    ....
}

//Example #2b:
class Foo{
    // Screw it... no need to use property prefixing
    public $make;
    private $model;
    protected $year;
    ....
}

 

 

Question #3: With public methods, do you explicitly declare public ones as such, or do you omit the public keyword?

//Example #3a:
class Foo{
    public function bar(){
....
    }
}

//Example #3b:
class Foo{
    function bar(){
....
    }
}

 

Finally, Question #4: Do you use upper / lower camel casing for class, property and method names?

//Example #4a:
class Foo{
    public $myVar;
    public function myMethodRocks(){
....
    }
}

//Example #4b:
// Screw camel case (never liked camels anyway)
class foo{
    public $myvar;
    public function mymethodrocks(){
....
    }
}

 

[ot]

For the record, I found myself:

  • doing both #1a and #1b (although I tend to lean more towards #1b)
  • trying to get into the habit of #2a
  • use the format of #3a
  • use the format of #4a

[/ot]

Link to comment
Share on other sites

Very interesting topic, I'm looking forward to seeing some of the responses.

 

#1: I tend to go with method #1a, I just format it a bit differently. I tend to do something like this (especially when there are a lot of properties):

 

class Foo
{
    public 
        $myVar1,
        $myVar2;
    ....
}

 

#2: I generally stick to #2a and follow the naming conventions, but I have to admit I sometimes do deviate from this just because I don't like it that much, and I don't find it to be too helpful. I tend to only do this on quick classes/projects, though.

 

#3: I always follow #3a and never omit the visibility keyword when declaring public methods, it would look too sloppy and inconsistent for my coding OCD to handle.  :-\

 

#4: I follow #4a and name them correctly because it's just more trouble not to, and it's really just a habit now so it's not really something that I would even consider not doing.

Link to comment
Share on other sites

1 - b

2 - neither.  Public entities begin with a capital letter.  The others start lower case.

3 - a

4 - a

 

Some people hate camel case.  Those people are insane.  I find it much less annoying than using underscores in variable and method names.  It also looks more professional to me.

Link to comment
Share on other sites

Here is how I do it:

 

#1:

class Foo{
    public $myVar01;
    public $myVar02;
    ....
}

 

#2:

class Foo{
    // Screw it... no need to use property prefixing
    public $make;
    private $model;
    protected $year;
    ....
}

The only time I prefix a variable is for global variables (such as: $_id = $_SESSION['id'])

 

#3:

class Foo{
    public function bar(){
....
    }
}

 

#4

class Foo{
    public $myVar;
    public function myMethodRocks(){
....
    }
}

Link to comment
Share on other sites

class SomeClass {

    public $somePublic, $someOtherPublic;

    private $somePrivate;

   

    public someMethod() {

 

    }

 

    private somePrivMethod() {

 

    }

}

 

Sometimes I'll prefix private methods with _ if there's also a public method with that name (obviously since I have to) or if I think someone else might ever modify the code.

 

 

Also, with camel case I'm entirely random.  Sometimes I use it, and sometimes I don't.  I do try to be consistent with camel case in projects though.

Link to comment
Share on other sites

Interesting responses.. ldb358 mentioned using underscores as opposed to camel casing.. (and have seen other examples on the interweb using underscores as well) must have slipped my mind while doing question 4.. so yeah, perhaps there could have been something along the lines of:

 

//Example #4c:
class Foo{ // or foo
    public $my_var;
    public function my_method_rocks(){
....
    }
}

 

In any case, it's interesting to see the various preferences / opinions to say the least.  :D

Link to comment
Share on other sites

1: #1b

It is just easier and allows for comments on each variable if needed.

 

2: #2b

I do not see how making them with _ helps any (other than to the person looking over the code), just an extra character.

 

3: #3a

Might as well conform to the standards and use the keywords that they created for you, which will also potentially add security to your script.

 

4: #4a

camelCase, just from my Java days it was a habit and it just makes it easier if you ask me to do and helps make less mistakes.

 

<?php

class myClass {
public $myVar1;
private $myVar2;
protected $myVar3;

public function myFunction() {

}
}
?>

Link to comment
Share on other sites

After all nothing stops you from assuming this convention:

 

<?php class $a {public $a; public $b; public $z public function __construct($a,$b,$c = null) {$this->a = $a; $this->b = $b; if(isset($c)) $this->z = $c}}

Link to comment
Share on other sites

After all nothing stops you from assuming this convention:

 

<?php class $a {public $a; public $b; public $z public function __construct($a,$b,$c = null) {$this->a = $a; $this->b = $b; if(isset($c)) $this->z = $c}}

 

Yeah. That's called the "job security" convention.

 

lol If I was in the industry as a lead developer, and saw someone within the project with coding convention like that, their heads would roll.. uphill.. with a 9 iron.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.