#### 1. 重寫規(guī)則 --- 一、重寫的類成員訪問權(quán)限不能低于父類 二、 重寫的類成員是不是靜態(tài)成員必須和父類保持一致 三、重寫方法時,參數(shù)類型必須保持一致,參數(shù)數(shù)量可多不可少,默認(rèn)值可多不可少 #### 2. 重寫的類成員訪問權(quán)限不能低于父類 --- 致命錯誤:用戶::$name的訪問級別必須是public(如在class Base中),位于E:\www\1.php的第15行 ``` Fatal error: Access level to User::$name must be public (as in class Base) in E:\www\1.php on line 15 ``` ``` class Base { public $name = '張三'; } class User extends Base { protected $name = '李四'; public function main() { echo $this->name; } } (new User)->main(); ``` #### 3. 重寫的類成員是不是靜態(tài)成員必須和父類保持一致 --- 致命錯誤:無法在E:\www\1.php的第15行將非靜態(tài)屬性 $name重新聲明為靜態(tài)屬性 $name ``` Fatal error: Cannot redeclare non static Base::$name as static User::$name in E:\www\1.php on line 15 ``` ```php class Base { public $name = '張三'; } class User extends Base { public static $name = '李四'; public function main() { echo $this->name; } } (new User)->main(); ``` #### 4. 方法參數(shù)類型必須保持一致 --- 警告:User::main(int$id)的聲明應(yīng)與E:\www\1.php第14行中的Base::main(string$id)兼容 ```php Warning: Declaration of User::main(int $id) should be compatible with Base::main(string $id) in E:\www\1.php on line 14 ``` ``` class Base { public function main(string $id) { } } class User extends Base { public function main(int $id) { } } ``` #### 5. 方法參數(shù)數(shù)量大于父類方法參數(shù)數(shù)量時, 參數(shù)必須有默認(rèn)值 --- 警告:User::main(int$id,string$name)的聲明應(yīng)與E:\www\1.php第16行中的Base::main(int$id)兼容 ``` Warning: Declaration of User::main(int $id, string $name) should be compatible with Base::main(int $id) in E:\www\1.php on line 16 ``` ``` class Base { public function main(int $id) { } } class User extends Base { public function main(int $id, string $name) { } } ``` #### 6. 父類方法參數(shù)有默認(rèn)值時, 子類方法必須也有默認(rèn)值 --- 警告:User::main(string$name)的聲明應(yīng)與E:\www\1.php第15行中的Base::main(string$name='')兼容 ``` Warning: Declaration of User::main(string $name) should be compatible with Base::main(string $name = '') in E:\www\1.php on line 15 ``` ```php class Base { public function main(string $name = '') { } } class User extends Base { public function main(string $name) { } } ```