- 不能实例化
- 使用 extends 继承
- 一个类只能继承一个class 或 abstract class
- 并非所有的方法都是抽象,只有abstract的方法(必须在子类实现)才是抽象方法,非抽象方法,子类可直接使用
- 抽象类的抽象方法只能使用public 和 protected 修饰
<?php
abstract class A {
//子类可直接使用
public function v1() {
echo "view A v1".PHP_EOL;
}
//子类可直接使用
protected function v2() {
echo "view A v2".PHP_EOL;
}
//不能使用private修饰,不然报错
/*
PHP Fatal error: Abstract function A::v3() cannot be declared private
*/
//abstract private function v3();
//非抽象子类 一定需要实现,不然报错
/*
PHP Fatal error: Class Aa contains 2 abstract methods and must therefore be declared abstract or implement the remaining methods (A::v4, A::v5)
*/
abstract public function v4();
abstract protected function v5();
}
class B {
}
//不会报错
abstract class Ab extends A {
}
//一定要实现方法 v4 和 v5,不然报错
/*
PHP Fatal error: Class Ac contains 2 abstract methods and must therefore be declared abstract or implement the remaining methods (A::v4, A::v5)
*/
/*
class Ac extends A {
}
*/
//一定要实现方法 v4 和 v5,不然报错
/*
PHP Fatal error: Class Ad contains 2 abstract methods and must therefore be declared abstract or implement the remaining methods (A::v4, A::v5)
*/
/*
class Ad extends Ab {
}
*/
//只能继承一个class 或 abstract class
class Aa extends A {
public function v4() {
echo "view A v4".PHP_EOL;
}
protected function v5() {
echo "view A v5".PHP_EOL;
}
public function test() {
$this->v1();
$this->v2();
//$this->v3();
$this->v4();
$this->v5();
}
}
//不能实例化
/*
PHP Fatal error: Uncaught Error: Cannot instantiate abstract class A
*/
//$a = new A();
$a = new Aa();
$a->test();