Example #1
0
echo "类的继承<br/>";
$b = new B();
//$b -> operation2();  Fatal error: Call to protected method A::operation2() from context ''
$b->say_something();
$b->lila = 10;
$b->do_something();
$b->lilb = 10;
class C
{
    public $attribute = "default value";
    function operation()
    {
        echo "Something<br/>";
        echo "The value of \$attribute is " . $this->attribute . "<br/>";
    }
}
class D
{
    public $attribute = "different value";
    function operation()
    {
        echo "Something else<br/>";
        echo "The value of \$attribute is " . $this->attribute . "<br/>";
    }
}
echo "方法与属性的重载<br/>";
$c = new C();
$c->operation();
$d = new D();
$d->operation();
//parent::operation();
Example #2
0
    public $test2 = 'Java';
    public $test3 = 'C++';
}
$a = new A();
foreach ($a as $output) {
    echo $output . '<br />';
}
//类的继承关系
class B
{
    public $test = "very large";
    function operation()
    {
        echo "welcome.<br /><br />";
        echo "The value of \$test is {$this->test}. <br /><br />";
    }
}
class C extends B
{
    public $test = "perfect";
    function operation()
    {
        echo "welcome back <br /><br />";
        echo "The value of \$test is {$this->test} <br /><br />";
    }
}
$a = new B();
$a->operation();
$b = new C();
$b->operation();