You can call a constructor for a parent class by this way:
Parents:: constructor($value)
In WordPress, when dealing with object-oriented programming, particularly when extending classes, you can call a constructor for a parent class using the parent::__construct()
method. This method is used within the constructor of the child class to invoke the constructor of the parent class. This ensures that any initialization logic present in the parent class constructor is executed before the child class constructor.
Here’s an example:
class Parent_Class {
public function __construct() {
echo "Parent class constructor called.";
}
}
class Child_Class extends Parent_Class {
public function __construct() {
parent::__construct(); // Calling parent class constructor
echo "Child class constructor called.";
}
}
$child_object = new Child_Class(); // Output: "Parent class constructor called.Child class constructor called."
In this example, when you create an instance of Child_Class
, the constructor of the parent class Parent_Class
is automatically invoked first due to the parent::__construct()
call within the Child_Class
constructor.