What is Inheritance:
Inheritance allows a class (subclass or child class) to inherit properties and methods from another class (superclass or parent class). It promotes code reuse and the creation of hierarchical relationships between classes.
Calling the Parent Constructor:
In object-oriented programming, when you extend a class (create a subclass or child class), you may need to call the constructor of the parent class (superclass) to ensure that any initialization tasks defined in the parent constructor are executed.
In PHP, you can call the constructor of the parent class from the constructor of the child class using the parent::__construct() syntax. This allows you to execute the parent constructor’s code before or after additional initialization tasks specific to the child class.
Here’s how you call the parent constructor in PHP:
<?php
class ParentClass {
public function __construct() {
echo "Parent constructor called";
}
}
class ChildClass extends ParentClass {
public function __construct() {
parent::__construct(); // Calling parent constructor
echo "Child constructor called";
}
}
$child = new ChildClass();
?>
Output:
Parent constructor called
Child constructor called
In the example above, when you create an instance of ChildClass, PHP automatically calls the constructor of ChildClass. Inside the constructor of ChildClass, parent::__construct() calls the constructor of ParentClass, ensuring that initialization tasks defined in the parent class are executed before any additional tasks defined in the child class.
Calling the parent constructor is crucial for maintaining proper object initialization and ensuring that inherited properties and methods are initialized correctly in PHP inheritance hierarchies.
Overriding Methods:
In object-oriented programming, method overriding is the ability to define a new implementation for a method in a subclass that already exists in its superclass. This allows subclasses to provide specialized behaviour while still maintaining a relationship with the superclass.
In PHP, method overriding is achieved by declaring a method in the subclass with the same name, parameters, and return type as the method in the superclass. When an object of the subclass is instantiated and the method is called, PHP invokes the overridden method from the subclass, rather than the method from the superclass.
Here’s a basic example of method overriding in PHP:
<?php
class Animal {
public function makeSound() {
return "Animal makes a sound";
}
}
class Dog extends Animal {
public function makeSound() {
return "Dog barks";
}
}
$animal = new Animal();
echo $animal->makeSound(); // Output: Animal makes a sound
$dog = new Dog();
echo $dog->makeSound(); // Output: Dog barks
In the example above, the makeSound() method is overridden in the Dog class. When makeSound() is called on an instance of Dog, PHP invokes the overridden method from the Dog class instead of the method from the Animal class.
Key Points about Method Overriding:
Method overriding allows subclasses to provide their own implementation of methods defined in the superclass.
The method signature (name, parameters, return type) must match exactly between the superclass and the subclass.
Method overriding is used to implement polymorphic behaviour, where different objects respond differently to the same method call.
Method overriding is a fundamental concept in object-oriented programming, allowing for the extension and customization of behaviour in subclasses while maintaining a consistent interface across the inheritance hierarchy.
The protected Keyword:
In PHP, the protected keyword is an access modifier used to restrict the visibility of properties and methods within a class hierarchy. Properties and methods declared as protected can be accessed within the class itself and its subclasses (child classes), but not from outside the class hierarchy.
Here’s a breakdown of how the protected keyword works:
Access within the Class Hierarchy:
Properties and methods marked as protected can be accessed from within the class where they are declared and from any subclasses that inherit from that class.
No Access from Outside the Class Hierarchy:
protected members are not accessible from outside the class where they are declared, including instances of the class and code that exists outside the class hierarchy.
Example:
<?php
class ParentClass {
protected $protectedProperty = "I am protected";
protected function protectedMethod() {
return "This is a protected method";
}
}
class ChildClass extends ParentClass {
public function accessProtected() {
echo $this->protectedProperty; // Accessing protected property from subclass
echo $this->protectedMethod(); // Accessing protected method from subclass
}
}
$obj = new ChildClass();
$obj->accessProtected(); // Output: I am protected This is a protected method
// Attempting to access protected property and method from outside the class hierarchy will result in a fatal error
// echo $obj->protectedProperty;
// echo $obj->protectedMethod();
In the example above, the protectedProperty and protectedMethod() are declared in the ParentClass with the protected access modifier. They are accessible within the ChildClass because ChildClass extends ParentClass.
Use Cases for protected:
Use protected properties and methods when you want them to be accessible within the class hierarchy but not from outside the hierarchy.
protected members are often used for implementing shared behaviour or state that is specific to a class and its subclasses.
By using the protected keyword, you can control access to class members, ensuring that only relevant parts of your class hierarchy can interact with and manipulate certain properties and methods.