OOPS in PHP 5 Tutorial - Parent Keyword

This article will guide you through the PARENT keyword used in OOPS implementation in PHP5. Parent Keyword allows to forcefully call the parent method and not the child method. This is useful when you are implementing Polymorphism in PHP5. This keyword is also very useful if you want the derived class to call the parent class before performing certain operation.





Example - Implementing Parent Keyword

< ?
class Person
{
      public function showData()
      {
            echo "This is Person's showData()n";
      }
}
 
class Customer extends Person
{
      public function showData()
      {
            parent::showData();  //Calling Person Class showData Method
            echo "This is Customer's showData()";
      }
}
 
$c = new Customer();
$c->showData();
?>

Output:
This is Person’s showData()
This is Customer’s showData()
Explanation:

Similar Posts

Custom Search

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments

:) Alot of information, I have hever seen before a such useful thing. Thank you :)

Leave a comment

(required)

(required)