#### 1. 重写规则 --- 一、重写的类成员访问权限不能低于父类 二、 重写的类成员是不是静态成员必须和父类保持一致 三、重写方法时,参数类型必须保持一致,参数数量可多不可少,默认值可多不可少 #### 2. 重写的类成员访问权限不能低于父类 --- 致命错误:用户::$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. 重写的类成员是不是静态成员必须和父类保持一致 --- 致命错误:无法在E:\www\1.php的第15行将非静态属性 $name重新声明为静态属性 $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. 方法参数类型必须保持一致 --- 警告:User::main(int$id)的声明应与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. 方法参数数量大于父类方法参数数量时, 参数必须有默认值 --- 警告:User::main(int$id,string$name)的声明应与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. 父类方法参数有默认值时, 子类方法必须也有默认值 --- 警告:User::main(string$name)的声明应与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) { } } ```