Esempio n. 1
0
<?php

class Car
{
    var $wheels = 4;
    var $door = 4;
    function DysplayValues()
    {
        return $this->wheels . " and " . $this->door . "<br/>";
    }
}
class CompactCar extends Car
{
    var $wheels = 2;
    var $door = 2;
    function DysplayValues()
    {
        return "Overriding values and function: " . $this->wheels . " and " . $this->door . "<br/>";
    }
}
$car1 = new Car();
$car2 = new CompactCar();
echo "Car1 : " . $car1->DysplayValues();
echo "Car2 : " . $car2->DysplayValues();
echo "<br/>";
echo "Car parent: " . get_parent_class("Car") . "<br/>";
echo "Compact Car parent: " . get_parent_class("CompactCar") . "<br/>";
echo is_subclass_of("CompactCar", "Car") ? "true" : "false";
Esempio n. 2
0
        return $this->wheels + $this->doors;
    }
}
class CompactCar extends Car
{
    // 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>";
Esempio n. 3
0
class Car
{
    var $wheels = 4;
    var $doors = 4;
    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*/