OOP Coding Standards
Posted: Thu Sep 04, 2008 1:48 pm
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?
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.
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
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.