HomePHP Using Inheritance, Polymorphism and Serialization with PHP Classes
Using Inheritance, Polymorphism and Serialization with PHP Classes
If you are working with classes in PHP, you will sooner or later encounter inheritance, polymorphism, and serialization. The ability to use these three will help speed up your code writing. This article covers how to use them, and more.
Inheritance, in object oriented terms, allows us to create subclasses of a given class. Think of it as a hierarchy of different kinds of classification. For example, in our previous article we used a class called human. Humans come in different forms; you have Africans, Europeans, Asians, and so forth. They all have similar attributes, e.g. legs, arms, eyes, and so on, but are different in other ways. Now, to translate these similarities and differences into code, we would use inheritance. In addition to inheritance we are going to discuss both polymorphism and serialization, among other topics.
Inheritance
The syntax for creating an inherited class is:
Class new_class_nameextendsoriginal_class_name{}
Let's look at our human class. The class as it stands now is very broad. Say we want to specify an African person with this class. This is what we will do:
class human{ function human($hcolor){ $this->hcolor=$hcolor; } var $legs=2; function report(){ return "This <b>".get_class($this)."</b> has <b>" .$this- >hcolor. "</b> hair,and <b>" .$this->legs. "</b> legs<br>" ; } } class African extends human{ } //instantiate the class $jude = new human("black"); $jane = new african("brown"); $jude->legs++; //increase legs by one echo $jane->report(); echo $jude->report();
In the above code we added a new class (African) which inherited everything from the human class. And we created a instance of this new class called jane. This is what we get when we run this code:
As you can see from the result, the new class name (african) is now shown. There is virtually no difference between the African class and the human class. This is because the extends keyword is causing the new class to inherit everything from the human class. So, the African is in effect, at the moment, the same as human. So how do we make differences between the two? We do this by specifying, within the new African class, certain variations:
class human{ function human($hcolor){ $this->hcolor=$hcolor; } var $legs=2; function report(){ return "This <b>".get_class($this)."</b> has <b>" .$this- >hcolor. "</b> hair,and <b>" .$this->legs. "</b> legs<br>" ; } } class African extends human{ var $hcolor=”black”; function African() {} } //instantiate the class $jude = new human("black"); $jane = new african(); $jude->legs++; //increase legs by one echo $jane->report(); echo $jude->report();
In the code above we added a variable called hcolor. We did this because the African class represents a new type of human. Now, we need to disable the constructor function human, because the African class inherits everything from the human class including its constructor function, so we need to create an equivalent function in the African class. So we add:
function African() {} }
This effectively overwrites the constructor function of the human class. Note that this function does not take any arguments.
As the results above show, the hair color for the African class is black. It will always be black no matter what we add to the jane object, because the hair color specified in the new African class is declared as black.