More than one way to do it
The following three chunks of code all do the same thing. Is there any circumstance under which they wouldn't?
(Using PHP5)
No constructor, instance variables set immediately inside class closure:
class A
{
private $number = 23;
// etc...
}
Constructor named "__construct":
class A
{
private $number;
public function __construct()
{
$this->number = 23;
}
// etc...
}
Constructor named the same as the class:
class A
{
private $number;
public function A()
{
$this->number = 23;
}
// etc...
}
That is, is there any time when setting an instance variable the first way would not have the same effect as doing it in the constructor? And is there any reason to use the class name instead of "__construct", or vice-versa?
Just curious. Thanks.
