示例#1
0
// $car->wheels;
// However, if the variable or method is 'static' we can access it directly in the class without an instance
// Note: with static methods you can't use $this!
class Vehicle
{
    public static $wheels = 4;
    public static function lorry()
    {
        // $wheels = 12; // If a NORMAL var is declared here, it can be accessed normally; but mixing normal and static vars and methods is probably not a good idea.
        // If trying to use a static variable, you must use the static :: notation, even within a method
        echo Vehicle::$wheels . "<br>";
    }
}
echo Vehicle::$wheels . "<br>";
Vehicle::lorry();
Vehicle::$wheels = 18;
echo Vehicle::$wheels . "<br>";
Vehicle::lorry();
echo "<hr>";
// Using static variables with class extension is NOT the same as using different instances
// Take this example:
class One
{
    static $foo;
}
class Two extends One
{
}
class Three extends One
{
}