EXTENDING A CLASS INTRODUCTION
The Course class is the class we use to create backend or frontend courses. We may use specialized public methods and properties for Frontend and Backend courses to make the class more useful. First this way of developing software implies that the Course class will grow very quickly, second if we create an object for the Frontend that object will be able to access methods and properties that are not necessary. The biggest problem is that we are mixing properties and methods specific to the Backend and Frontend courses.
THE BACKEND AND FRONTEND CLASSES
We could create two classes one for the Backend and one for the Frontend with specific properties and methods. But suppose the Corso class has a method that we need for both classes, under these conditions we would have to duplicate the method twice. This is not a good practice, code duplication is always to be avoided. Let’s leave the Course class as it is and create two specific classes, a Backend class and a Frontend class. The Corso class must be extended by inheriting the summary method which will be used by both classes.
THE INHERITANCE
To do this, we take advantage of one of the pillars of OOP which is inheritance through the extends keyword positioned after the class name. We can inherit attributes and methods of the Course class that have been indicated with public or protected visibility. This class is called superclass while the other two classes are called subclasses.
$php = new Backend();
$css = new Frontend();
var_dump ($css, $php);
The image opposite shows the result of the var_dump. As can be seen, all the properties of the Corso class have been inherited, but obviously not initialized since the constructor of the Corso class has not been invoked.
Another important detail to know is that PHP does not support multiple inheritance, that is, we can only inherit from a superclass.
INVOKE THE PARENT CONSTRUCTOR
When we create an instance of the child class, the constructor of that class will be invoked but not the constructor of the parent class. If we remove the constructor within the subclass, for example, Frontend will invoke the parent constructor. If from the subclass we want to invoke a method of the parent class we have to use the keyword parent.
PUBLIC PROTECTED PRIVATE VISIBILITY
A property or method declared as public is accessible anywhere. protected is visible to child classes (subclasses of the parent class) but is not accessible externally to properties and methods via instances, a private property or method is not visible in child classes or externally via instances. If within the superclass we have indicated a method as public, also in the subclasses it must be public otherwise we will get a fatal error. On the other hand, if within a superclass we give protected visibility, in the subclasses we can use the same scope of visibility or greater (public).
NAMED ARGUMENTS
In PHP 8 thanks to the named arguments we can use this syntax to initialize the constructor. The order of the arguments does not matter.
$ corsoPHP = new Course ( prezzo: 19, titolo: “PHP” );
In this case we have reversed the price and title.
COMPLETE CODE OF THE CLASSES USED
GETTER AND SETTER METHODS
A getter method returns the value of a property, while a setter sets the value of a property. Thanks to the typed properties introduced in PHP 7.4 we can avoid the getter and setter methods.
TYPED PROPERTIES
Thanks to the typed properties introduced in PHP 7.4 we can avoid the getter and setter methods.
PROPERTY READ-ONLY
From PHP 8.1 we can make the properties read-only thanks to the readonly keyword (See price properties of the Course class).
FURTHER INFORMATION
In PHP, the concepts of “typed properties” and “read-only” were introduced to improve the handling of object-oriented code by allowing more precise control over the type of data and access to the properties of a class.
Let’s look at these concepts in detail.
1. Typed Properties (Typed Properties)
Typed properties were introduced in PHP 7.4.
They allow you to declare a type for the properties of a class.
When using a typed property, PHP requires that the value assigned to the property be of the specified type, otherwise an error is generated.
Example of typed properties:
class User {
public string $name;
public int $age;
public function __construct(string $name, int $age) {
$this->name = $name;
$this->age = $age;
}
}
$user = new User(“Alice,” 30);
echo $user->name; // Alice
In this example, the $name property is of type string, and $age is of type int.
PHP ensures that only values of the correct type can be assigned to these properties.
2. Read-Only Properties
Starting with PHP 8.1, the ability to declare “read-only” properties was introduced.
A read-only property can be initialized only once and cannot be modified later.
This is useful when you want to ensure that a property retains the same value throughout the life of the object.
Example of read-only properties:
class User {
public readonly string $name;
public readonly int $age;
public function __construct(string $name, int $age) {
$this->name = $name;
$this->age = $age;
}
}
$user = new User(“Alice,” 30);
echo $user->name; // Alice
// Attempting to modify the property after its initialization will generate an error// $user->name = “Bob”; // Fatal error: Cannot modify readonly property User::$name
3. Parent Constructor
The parent constructor is used to call the parent class constructor from a child class.
It is useful when the daughter class extends a parent class and you want to maintain the behavior of the base class constructor, possibly by adding additional initializations in the daughter class.
Example of using the parent constructor:
class Person {
protected string $name;
public function __construct(string $name) {
$this->name = $name;
}
}
class Employee extends Person {
private int $salary;
public function __construct(string $name, int $salary) {
parent::__construct($name); // Call the constructor of the parent class
$this->salary = $salary;
}
}
In this example, the Employee class extends the Person class.
The constructor of Employee uses parent::__construct($name) to call the constructor of the Person class, ensuring that the $name property is initialized correctly, as well as initializing the $salary property specific to the Employee class.
Summary
– Typed Properties: Allow a type to be defined for class properties, ensuring that only values of that type are accepted.
– Read-Only Properties: Properties that can be initialized once and cannot be changed later.
– Parent Constructor: Used in derived classes to call the constructor of the base class, maintaining and extending initialization behavior.
These concepts improve the robustness and security of the code, reducing errors and unexpected behavior.
Leave A Comment