ok.. so I'm am learning classes.. and I have some simple (and perhaps negligable questions)
here is a small simple example snippet:
class person{
public $name;
public $age;
public $weight;
public function __construct(){
$this->age = 0;
$this->weight = 0;
}
}
My first question is with regards to constructs.. I read that if you have a construct in your class, an object cannot be instantiated until the construct executes.. In this case, there is no parameters to pass along into the construct.
so when I create Bob I don't see a difference between,
$bob = new person;
-and-
$bob = new person();
I suppose in this case, it doesn't really matter since the __construct() doesn't pass in any parameters? From good coding practices, which version of creating object 'bob' is better?
Finally, when it comes to setting up default properties within a class.. I realise I can go a few ways.. so in the above class example, I want the default name property to be unknown.. does it matter whether I do this:
class person{
public $name;
public $age;
public $weight;
public function __construct(){
$this->name = 'UNKNOWN';
$this->age = 0;
$this->weight = 0;
}
}
or this?
class person{
public $name = 'UNKNOWN';
public $age;
public $weight;
public function __construct(){
$this->age = 0;
$this->weight = 0;
}
}
Your wisdom and insight is appreciated 🙂
Cheers,
NRG