Page 1 of 1

OOP Coding Standards

Posted: Thu Sep 04, 2008 1:48 pm
by The_Anomaly
Well, I've been searching around a lot, and didnt' really find much, so I thought I'd ask on here.

I've recently discovered the joy that is OOP, and I'm struggling to find the right way to do it. I'm sure this varies greatly, but I want to learn what is generally considered best or standard practice when it comes to OOP. The main issues that I was wondering were regarding encapsulation, object calling, and such.

My first question is regarding variables. Is it generally best practice to assign all variables through arguments in a __construct method?

Code: Select all

 
class Peanut_Class
{
    public $variable_public;
    protected $variable_protected;
    
    function __construct($peanuts){
        $this->variable_protected = $peanuts;
        echo $this->variable_protected;
    }
}
$peanuts = new Peanut_Class('test');//"Test" is echoed
 
Or should I be using setters for defining the variables? Or should I not even define variables at all, and just whenever I need them get them into the method through arguments (Much like procedural programming). Or should I define the variables by using the object ($object->variable = "string") in the same file that we're defining the object? Basically, how do you deal with variables in methods?

That's just a small example. I'm just wondering, what are the best practices for OOP? With regard to inheritance, variables, autoloading, type hinting, etc?

I'm a newer programmer, so I'm trying to develop a library of classes that I can reuse thorughout this project and other projects to come. I'd just rahter do them the right way, than to have to redo it every time I see a better way. I know these things are not written in stone, but if any more experienced programmers can give me some advice, I'd really appreciate it.


Thanks.

Re: OOP Coding Standards

Posted: Thu Sep 04, 2008 1:53 pm
by Christopher
I'd recommend reading the Zend Framework guidelines.

Re: OOP Coding Standards

Posted: Fri Sep 05, 2008 8:15 am
by The_Anomaly
Thanks for the recommendation, but unless I'm looking at the wrong document, it seems those guidelines are more related to syntax and formatting (tab vs. four spaces) than the OOP standards I was hoping for. Is there anything else?

Re: OOP Coding Standards

Posted: Fri Sep 05, 2008 8:27 am
by jayshields
Correct me if I'm wrong, but he probably means not only to look at the Zend coding standards, but also at the actual code in the Zend framework. If anyones going to get it right, it's them.

Re: OOP Coding Standards

Posted: Fri Sep 05, 2008 10:41 am
by Christopher
The_Anomaly wrote:My first question is regarding variables. Is it generally best practice to assign all variables through arguments in a __construct method?

Or should I be using setters for defining the variables? Or should I not even define variables at all, and just whenever I need them get them into the method through arguments (Much like procedural programming). Or should I define the variables by using the object ($object->variable = "string") in the same file that we're defining the object? Basically, how do you deal with variables in methods?

That's just a small example. I'm just wondering, what are the best practices for OOP? With regard to inheritance, variables, autoloading, type hinting, etc?
Regarding the questions you are asking, first of all there is no one answer for all cases. For example, in general you should create objects that are initialized and useful. Therefore constructor args are a good practice to meet that goal. But obviously you can object to be able to change their behavior as necessary too.

And in general you should have methods where you are asking the object to do something rather than just using setters and then calling a methods like you would pass parameters in a procedural call. But setters are certainly useful in many cases. Likewise type hinting is useful in some circumstances, yet prohibits overloading and duck typing in others.

Inheritance is a huge subject, as is composition which is preferred these days. Neither is correct in all cases and external forces may make you select one over the other regardless of the best practice in the specific code. Similarly object properties are a big subject, and in many ways the whole purpose for objects -- because the whole point of objects is that they are data that has code associated with it, so the data is in many ways the essence of objects.

It takes years of solving actual problems that need these solutions to understand many of these concepts. The why is as important as the how. We can certainly address these questions one at a time. I would also recommend buying some books. There are a few threads around here with recommendations.

Re: OOP Coding Standards

Posted: Sat Sep 06, 2008 10:12 am
by panic!
I think it's better to assign the variables after the constructor where possible because it's more readable for someone who's not au fait with the project/class/code.

Code: Select all

 
 
$pet=new cat(10,20,'very'); //These arguments could be anything
 
$pet=new cat();
 
$pet->age=10;
 
$pet->weight=20;
 
$pet->hairyness='very';  // way more obvious what these variables are.
 
 
That's my personal preference anyway!