<?php
class A{
public $a1='a1';
protected $a2='a2';
function test(){
echo "hello!<hr/>";
}
}
class B extends A{//若A類和B類不在同一文件中 請包含后(include)再操作
public $a1='b1';
function test2(){
$this->test();
parent::test();//子類調用父類方法
}
function test()
{
echo $this->a1.',';
echo $this->a2.',';
echo "b2_test_hello<hr/>";
}
}
$a = new B();
$a->test();//b1,a2,b2_test_hello
$a->test2();//b1,a2,b2_test_hello//hello!
方法的調用:$this->方法名();如果子類中有該方法則調用的是子類中的方法,若沒有則是調用父類中的
parent::則始終調用的是父類中的方法。
變量的調用:$this->變量名;如果子類中有該變量則調用的是子類中的,若沒有則調用的是父類中的
