Will every class have a constructor method?
Not necessarily. A constructor is responsible for initializing the class in the act of instantiation. That is, when the new
operator is invoked together with the class name, __construct
is implicitly called to do the operations you defined on it.
In your example, __construct
is being used to pass arguments to your class and store it in properties.
A small example for you to understand is a class where you have Getters and Setters to set values to a property. However, the fills of these attributes need to be mandatory, as they are required by the class. In this case we can use __construtor
as follows:
class Person
{
protected $name;
protected $age;
public function __construct($name, $age)
{
$this->name = $name;
$this->age = $age;
}
/**
* Gets the value of name.
*
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* Sets the value of name.
*
* @param mixed $name the name
*
* @return self
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Gets the value of age.
*
* @return mixed
*/
public function getAge()
{
return $this->age;
}
/**
* Sets the value of age.
*
* @param mixed $age the age
*
* @return self
*/
public function setAge($age)
{
$this->age = $age;
return $this;
}
}
Note that $name
and $age
are arguments required for the constructor. Then it is necessary to inform them at the moment of the instance of the class, so that the construction occurs as desired:
$person = new Person('Wallace', 26);
The
__construtor
does not necessarily need arguments to work as a initializer, but it will make sense to use it if you have to do something "automatic" at instantiation of your class.
Example:
class Document
{
protected $createdAt;
public function __construct()
{
$this->createdAt = new \DateTime;
}
}
If you instantiate your class, note that it will automatically create the DateTime
object in the createdAt
property.
$document = new Document;
print_r($document);
Result:
Document Object
(
[createdAt:protected] => DateTime Object
(
[date] => 2016-11-16 11:31:42.000000
[timezone_type] => 3
[timezone] => America/Sao_Paulo
)
)
Why do I need it?
As stated earlier, to be able to define what will be done when the class was instantiated.
Of course you may not need it in some cases, but it will depend a lot on the architecture of the class you created.
There are classes in PHP that do not use constructors, such as stdClass
.
$object = new stdClass;
$object->id = 1;
% inherited%
There will be cases where you will not need to declare the constructor in the class because it has an inheritance from another that already has a constructor.
Look at the example below:
abstract class Animal
{
protected $name;
public function __construct($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
abstract public function getSound();
}
class Dog extends Animal{
public function getSound()
{
return 'au au';
}
}
$dog = new Dog('Billy');
$dog->getName(); // 'Billy'
Note that __construct
inherits an abstract class called Dog
. In this case, Animal
has a constructor to set the Animal
attribute to the instance act. But I did not have to declare the name
in Dog, because%% has these desired features.
Overwriting __construct
In PHP, Animal
can be overwritten by a class that inherits another.
See:
class A{
protected $argument;
public function __construct() {
echo "chamei a classe A";
}
}
class B extends A {
protected $argument;
public function __construct($argument)
{
echo "chamei a classe B com argumento $argument";
}
}
new A(); // "Chamei a classe A"
new B('oi'); // "Chamei a calsse B com argumento oi"
Important note is that when you override the constructor, it completely loses the behavior defined in the parent class (in our case it is the __construct
class).
In this case, if you need to define a constructor for the Daughter class, but need to call the parent class constructor, you can use __construct
to resolve this problem.
See:
class B extends A {
protected $argument;
public function __construct($argument)
{
parent::__construct();
echo "chamei a classe B com argumento $argument";
}
}