Using the extends keyword to define a subclass : Inheritance « Class « PHP






Using the extends keyword to define a subclass

 
<?php
class Cat {
    var $age;

    function Cat($new_age){
        $this->age = $new_age;
    }
    function Birthday(  ){

        $this->age++;
    }
}
class MyCat extends Cat {
    function MyCat(  ) {
    }
    function sleep(  ) {
        echo("Zzzzzz.<br />");
    }
}
$fluffy=new MyCat(  );
$fluffy->Birthday(  );
$fluffy->sleep(  );
echo "Age is $fluffy->age <br />";
?>
  
  








Related examples in the same category

1.subclass and parent class
2.Three levels of inheritance
3.extends and implement
4.Basic Inheritance
5.Class Inheritance
6.Class Member Binding in PHP
7.Creating a Class That Inherits from Another
8.Define an Executive class that inherits Employee
9.inheritance example
10.Example of Inheritance
11.Overriding parent methods
12.Using inheritance to efficiently represent various vehicle types
13.Using the parent construct