KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I was looking through the PHP documentation and saw several comments where a variable was initialized outside of a class's constructor, similar to the following: classMyClass { private $count = 0; public function __construct() { //Do stuff } } In PHP Objects, Patterns, and Practice , the author recommends using constructs only for the initialization of properties, deferring any heavy lifting or complex logic to specialized methods. This tutorial (a quick example that I found on Google) also recommends using constructors to initialize properties: http://www.killerphp.com/tutorials/object-oriented-php/php-objects-page-3.php . Why would you want to initialize a variable outside the constructor? Is this just sloppy coding, or is there a reason to do something like this? I have to say that until recently, I initialized default values outside the constructor, and there doesn't seem to be any programmatic advantage of one way over the other.
Tags (comma-separated)
Save Edits
Cancel