Polymorphism-Inheritance in PHP

PHP & MySQL Course:Polymorphism-Inheritance in PHP

In the previous lecture 8, we learnt about OOP in PHP programming. We deal with classes and objects

in OOP as we learnt in the lecture 8.

Lecture 9:Polymorphism-Inheritance in PHP

In this lecture we will also follow the class and objects. But in this lecture we will discuss the

concept of following topics:

  1. Inheritance in PHP with example
  2. Polymorphism in PHP
  3. $this variable in PHP

Inheritance in PHP with exaples:

When we use the concept of the inheritance, at that time we declare a base class and after that

we declare a drive class same like the base class. We can create or add methods or functionalities in the

drive class in case of base class. The inherit concept extends the communication of two classes. For

example there are two classes A and B. in this case the B is drive from the A class. Now lets see the

listing 1 for details about the Inheritance and we will use a keyword as extends.

Listing 1: Inheriting the classes


<?

class Labtop {

private $Model = "Dell Studio 1569";

private         $HardDrive = 500;

private         $Ram = 8;

public function Specification(){

return $this->Model;

}

}

class User extends Labtop{

public $UserName = "Ashley";

public $UserExperince = "Engineering";

public function UserInformation(){

echo $this->UserName." has a ";

echo $this->Specification();

}

}

$obj = new User;

echo $obj->UserInformation();

?>

In this example there are two classes first class is Labtop which is base class and the other class is User

which is derived class. And we inherit them by using the keyword extends, so by this way we can inherit

the classes in PHP programming.

Polymorphism in PHP:

First of all we should know, what is polymorphism? So dear friend’s polymorphism means that many

forms. In simple words there are many methods but they are sharing same interface. And in PHP the

polymorphism is not possible due to some problems. But in PHP5 there is a way to use the concept

of the Polymorphism in PHP programming for dynamic websites. Lets check the listing 2 for details

about the polymorphism in PHP programming.

Listing 2: Polymorphism in PHP


<?

class Labtop {

public function Specification() {

echo "Labtop Class method called...";

}

}

&nbsp;

class User extends Labtop  {

public function Specification() {

echo "User Class method called";

}

}

&nbsp;

function CallingMethods(Labtop  $call) {

$call->Specification();

}

&nbsp;

$Object = new User();

CallingMethods($Object);

?>

We used the same class of listing 1, but here in listing 2 we apply the polymorphism concept as you can see it.

And now check the listing 3 for details about the overridden of methods in PHP using the polymorphism.

Listing 3: Overridden in PHP


<?

class Labtop {

public function Specification() {

echo "Labtop Class method called...";

}

}

&nbsp;

class User extends Labtop  {

public function Specification() {

echo "User Class method called";

}

}

&nbsp;

function CallingMethods(Labtop  $call) {

$call->Specification();

}

&nbsp;

//$Object = new User();

$Object = new Labtop ();

//CallingMethods($Object);

$Object->Specification();

?>

In this case the methods are overridden and just one method is being called due the object of the class.

In the listing 2, we declared a method for calling the methods that is also calling the same thing which

we done in the listing 3. So it is cleared that listing 2 and listing 3 are overridden methods and the

polymorphism is not possible in PHP.

Summary:

In this lecture learnt about the inheritance and polymorphism. We will also use these two terms in our

coming lectures more, here we just learnt some introduction about them. And I also used the $this variables

which you can check it in the listing 1.

These two topics are the heart or you can say that are the soul of the OOP. Because OOP depends on

class and objects. And here in class and objects are just to solve the problems. We will create little

projects using the PHP programming, which will clear our concepts which we learnt in this PHP

and MySQL course.

admin
 

student of computer Science , having interest and experience in C++,Java,Php,Html,Css,JavaScript, wordpress and android development