Powered By Blogger

Monday, 8 August 2011

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:
  • Here i am extending the Person Class in Customer Class
  • In Customer Class showData() method is calling the parent class showData() method


No comments:

Post a Comment