nrg_alpha Posted January 3, 2010 Share Posted January 3, 2010 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] Quote Link to comment https://forums.phpfreaks.com/topic/186990-oop-convention-how-do-you-do-it/ Share on other sites More sharing options...
Alex Posted January 3, 2010 Share Posted January 3, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/186990-oop-convention-how-do-you-do-it/#findComment-987455 Share on other sites More sharing options...
ldb358 Posted January 3, 2010 Share Posted January 3, 2010 1) i use #1b just how ive always done it 2)i use #2b 3) i use #3a find it easier to read 4)i tend to use under scores as that's how i learned to code when my school taught me action script and it carried over to php Quote Link to comment https://forums.phpfreaks.com/topic/186990-oop-convention-how-do-you-do-it/#findComment-987471 Share on other sites More sharing options...
KevinM1 Posted January 3, 2010 Share Posted January 3, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/186990-oop-convention-how-do-you-do-it/#findComment-987550 Share on other sites More sharing options...
The Little Guy Posted January 3, 2010 Share Posted January 3, 2010 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(){ .... } } Quote Link to comment https://forums.phpfreaks.com/topic/186990-oop-convention-how-do-you-do-it/#findComment-987554 Share on other sites More sharing options...
corbin Posted January 3, 2010 Share Posted January 3, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/186990-oop-convention-how-do-you-do-it/#findComment-987559 Share on other sites More sharing options...
Daniel0 Posted January 3, 2010 Share Posted January 3, 2010 1b: It allows you to have docblock comments for the properties. 2a: As per the ZF coding standards. 3a: I think it makes it easier to read the code. 4a: Easier to read than not using any word separation. Easier to type than using underscores. Quote Link to comment https://forums.phpfreaks.com/topic/186990-oop-convention-how-do-you-do-it/#findComment-987565 Share on other sites More sharing options...
Andy-H Posted January 3, 2010 Share Posted January 3, 2010 I go with #1b #2b #3a #4a Quote Link to comment https://forums.phpfreaks.com/topic/186990-oop-convention-how-do-you-do-it/#findComment-987642 Share on other sites More sharing options...
nrg_alpha Posted January 3, 2010 Author Share Posted January 3, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/186990-oop-convention-how-do-you-do-it/#findComment-987685 Share on other sites More sharing options...
Adam Posted January 4, 2010 Share Posted January 4, 2010 I tend to go with: 1b 2b 3a 4a - variables I tend to separate with an underscore, camel case for methods. Quote Link to comment https://forums.phpfreaks.com/topic/186990-oop-convention-how-do-you-do-it/#findComment-988091 Share on other sites More sharing options...
premiso Posted January 5, 2010 Share Posted January 5, 2010 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() { } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/186990-oop-convention-how-do-you-do-it/#findComment-988570 Share on other sites More sharing options...
Daniel0 Posted January 5, 2010 Share Posted January 5, 2010 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. That's pretty much the point of all formatting conventions. The compiler/interpreter doesn't care either way. Quote Link to comment https://forums.phpfreaks.com/topic/186990-oop-convention-how-do-you-do-it/#findComment-988724 Share on other sites More sharing options...
Mchl Posted January 5, 2010 Share Posted January 5, 2010 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}} Quote Link to comment https://forums.phpfreaks.com/topic/186990-oop-convention-how-do-you-do-it/#findComment-988726 Share on other sites More sharing options...
Daniel0 Posted January 5, 2010 Share Posted January 5, 2010 Yeah. That's called the "job security" convention. Quote Link to comment https://forums.phpfreaks.com/topic/186990-oop-convention-how-do-you-do-it/#findComment-988730 Share on other sites More sharing options...
nrg_alpha Posted January 5, 2010 Author Share Posted January 5, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/186990-oop-convention-how-do-you-do-it/#findComment-988925 Share on other sites More sharing options...
Mchl Posted January 5, 2010 Share Posted January 5, 2010 Duh... I could remove even more whitespace from that and also omit optional keywords... Quote Link to comment https://forums.phpfreaks.com/topic/186990-oop-convention-how-do-you-do-it/#findComment-988945 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.