Inheritance

Inheritance in php

Concept of inheritance in php is as simple as in other oop languages as from php5 community target is to provide healthy oop concept. If you will analyze basic code of my previous topic, this is typical example of inheritance in php. To implementing inheritance in php you need at least 2 classes. One will be parent class and other will be child class. In child class you can inherit all properties and methods(protected and public only) from parent class. Now the advantages of inheritance is that there is no need to redefine the required properties of super class one again in sub class, since they could be inherited. That what, we have said that, inheritance increases reusability and reduces line of code and there by it increases simplicity among inter related classes.

Generally, inheritance has three types, single, multiple and multi level inheritance. But, PHP supports only single inheritance, where, only one class can be derived from single parent class. Even though, PHP is not supporting any other inheritance type except single inheritance, we can simulate multiple inheritance by using PHP interfaces.

In PHP, inheritance can be done by using extends keyword, meaning that, we are extending the derived class with some additional properties and methods of its parent class. The syntax for inheriting a class is as follows. Child_class_name extends Parent_class_name{…}

class Novel extends Books{ var publisher; function setPublisher($par){ $this->publisher = $par; } function getPublisher(){ echo $this->publisher. "<br />"; }}

Понравилась статья? Добавь ее в закладку (CTRL+D) и не забудь поделиться с друзьями:  



double arrow
Сейчас читают про: