Learning PHP and MySQL

Example 05-18 Using the parent construct

<?php
class  Cat  {
  
//  How  old  the  cat  is var  $age;

  
function  Cat($new_age)
  
{
    
//  Set  the  age  of  this  cat  to  the  new  age
    
$this->age  =  $new_age;
  
}
  
function  Birthday()
  
{
    $this
->age++;
  
}
  
function  Eat()
  
{
    
echo  "Chomp  chomp.";
  
}
  
function  Meow()
  
{
    
echo  "Meow.";
  
}
}

class  Domestic_Cat  extends  Cat  {
  
//  Constructor
  
function  Domestic_Cat()  {
  }

  
//  Eat  like  a  Domestic_Cat 
  
function  eat()  
  parent
::eat();
  
//  After  we're  finished  eating,  let's  meow
  
$this->meow();
  
}
}
?>

Posted by on 08/03 at 08:25 AM

Next entry: Example 05-19 Calling the constructor of the parent class

Previous entry: Example 05-17 Using the extends keyword to define a subclass

<< Back to main