Dino Geek, try to help you

What is inheritance in PHP?


Inheritance in PHP
Inheritance in PHP is an object-oriented programming concept that allows a class (referred to as a subclass or child class) to inherit properties and methods from another class (referred to as a superclass or parent class). The primary goal of inheritance is to promote code reusability and establish a hierarchical relationship among classes, thereby creating a clear and manageable structure for complex applications.

  1. How Inheritance Works in PHP
    In PHP, inheritance is facilitated using the `extends` keyword. When a class extends another class, it inherits all non-private properties and methods of the parent class. The syntax for creating a child class that inherits from a parent class is as follows:

```
class ParentClass { // Properties protected $property;

// Constructor public function __construct($value) { $this->property = $value; } // Method public function showProperty() { echo $this->property; } }

class ChildClass extends ParentClass { // Additional properties and methods (if any) public function extraMethod() { echo “This is an extra method in ChildClass.”; }
}
```

In this example, `ChildClass` extends `ParentClass` and thus inherits its properties and methods. The `ChildClass` can also have its own additional properties and methods, apart from those inherited from `ParentClass`.

  1. Overriding Methods
    A child class in PHP can override the methods of its parent class. This means the child class provides a specific implementation of a method that is already defined in its parent class. Consider the following example:

```
class ParentClass { public function displayMessage() { echo “Message from ParentClass.”; }
}

class ChildClass extends ParentClass { public function displayMessage() { echo “Message from ChildClass.”; }
}

$child = new ChildClass();
$child->displayMessage(); // Outputs: Message from ChildClass.
```

In this example, the `displayMessage` method in `ChildClass` overrides the same method in `ParentClass`. When the `displayMessage` method is called on an instance of `ChildClass`, the overridden method is executed.

  1. Accessing Parent Methods
    Even if a child class overrides a parent method, it can still access the parent’s method using the `parent::` keyword. For example:

```
class ParentClass { public function displayMessage() { echo “Message from ParentClass.”; }
}

class ChildClass extends ParentClass { public function displayMessage() { parent::displayMessage(); // Calls the parent class method echo “ Additional message from ChildClass.”; }
}

$child = new ChildClass();
$child->displayMessage(); // Outputs: Message from ParentClass. Additional message from ChildClass.
```

Here, `parent::displayMessage()` calls the `displayMessage` method from `ParentClass`, and then additional content is appended by the `ChildClass` method.

  1. Final Keyword
    PHP provides the `final` keyword to prevent a class from being inherited or to prevent a method from being overridden. This is useful for creating immutable class structures. For example:

```
final class ParentClass { // This class cannot be inherited
}

// or

class ParentClass { final public function displayMessage() { // This method cannot be overridden }
}
```

  1. Abstract Classes
    Abstract classes in PHP cannot be instantiated directly. They serve as a blueprint for other classes. A class defined as `abstract` contains abstract methods, which must be implemented by derived classes:

```
abstract class AbstractClass { abstract protected function displayMessage();
}

class ConcreteClass extends AbstractClass { public function displayMessage() { echo “Concrete implementation of abstract method.”; }
}
```

In this example, `ConcreteClass` extends `AbstractClass` and provides an implementation for the abstract method `displayMessage`.

  1. Conclusion
    Inheritance in PHP offers powerful mechanisms for code reuse, structure, and maintenance of large-scale applications. It enhances the modularity and efficiency of code by allowing hierarchical classification and implementation of shared functionality.

  1. Sources
    1. [PHP Manual: Classes and Objects – Inheritance](https://www.php.net/manual/en/language.oop5.inheritance.php)
    2. [PHP Manual: Final Keyword](https://www.php.net/manual/en/language.oop5.final.php)
    3. [PHP Manual: Abstract Classes](https://www.php.net/manual/en/language.oop5.abstract.php)


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use