KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Is there a reason to set a value for variables in the constructor of a class rather than when you declare them? I realize you can't pass data into the variables if you try to set them when they are declared, but what about things that will always be the same ( i suppse 'always' is a tough claim to make )? class variables_set_outside_constructor { private $admin_name = 'jeff'; private $current_working_directory = get_cwd(); function __construct() {} } as opposed to this: class variables_set_inside_constructor { private $admin_name; private $current_working_directory; function __construct() { $this->admin_name = 'jeff'; $this->current_working_directory = get_cwd(); } } What are the advantages and disadvantages of settings values in the constructor versus when they are declared? I'm curious about any language agnostic aspects as well.
Tags (comma-separated)
Save Edits
Cancel