Пример #1
0
{
    // CompactCar appears to have nothing in it. However that is the beauty of
    // coding and not REPEATING oneself!
    // In this case everything in this CompactCar class is the same as where
    // it became extended from (Car in this case)
    // However variables and methods of a class can be overridden by just
    // redeclaring the variable or method in the inheriting class
    //
    var $doors = 2;
}
$car1 = new Car();
$car2 = new CompactCar();
echo "Car 1 is: <br>";
echo $car1->wheels . "<br>";
echo $car1->doors . "<br>";
echo $car1->wheelsdoors() . "<br>";
echo "<br>";
echo "Car 2 is: <br>";
echo $car2->wheels . "<br>";
echo $car2->doors . "<br>";
echo $car2->wheelsdoors() . "<br>";
echo "<br>";
// Some useful functions when dealing with classes and the parents.
// This is for dynamic use
echo "Car parent:" . get_parent_class('Car') . "<br>";
echo "CompactCar parent:" . get_parent_class("CompactCar") . "<br>";
echo "<br>";
// reads and works like: 1st arg is a subclass of 2nd arg
echo is_subclass_of('Car', 'Car') ? 'true car for car' : 'false car for car';
echo "<br>";
echo is_subclass_of("CompactCar", "Car") ? "true compact for car" : 'false
Пример #2
0
    function wheelsdoors()
    {
        return $this->wheels + $this->doors;
    }
}
//Inheriting everything from Cars
class CompactCar extends Car
{
    var $doors = 2;
}
$car1 = new Car();
$car2 = new CompactCar();
echo "<h3>" . "Car 1" . "</h3>";
echo 'Number of wheels: ' . $car1->wheels . '<br>';
echo 'Number of doors: ' . $car1->doors . '<br>';
echo 'Sum of wheels and doors: ' . $car1->wheelsdoors() . '<br>';
echo '<hr>';
echo "<h3>" . "Car 2" . "</h3>";
echo 'Number of wheels: ' . $car2->wheels . '<br>';
echo 'Number of doors: ' . $car2->doors . '<br>';
echo 'Sum of wheels and doors: ' . $car2->wheelsdoors() . '<br>';
echo '<hr>';
/** Gets the parent class of Car - Null because car is already the parent */
echo "<b>Car is the Parent class: </b>" . get_parent_class('Car') . "<br>";
/** Gets the parent class of CompactCar */
echo "<b>Parent class of CompactCar: </b>" . get_parent_class('CompactCar') . "<br>";
/** Finding whether a parent is a subclass or not*/
echo is_subclass_of('Car', 'Car') ? 'true' : 'false';
//False
echo "<br>";
echo is_subclass_of('CompactCar', 'Car') ? 'true' : 'false';