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.