PHP继承与多态深入理解继承和多态是面向对象编程的核心概念。PHP的继承机制和C、Java有些不同。今天说说PHP中的继承和多态。基本的类继承。phpclass Animal{public function __construct(protected string $name) {}public function speak(): string{return 动物叫声;}public function getName(): string{return $this-name;}}class Dog extends Animal{public function speak(): string{return 汪汪!;}}class Cat extends Animal{public function speak(): string{return 喵喵!;}}$animals [new Dog(旺财), new Cat(咪咪)];foreach ($animals as $animal) {echo {$animal-getName()}: {$animal-speak()}\n;}?抽象类和接口。phpabstract class Shape{abstract public function area(): float;public function description(): string{return 一个面积为 . $this-area() . 的形状;}}interface Drawable{public function draw(): string;}class Circle extends Shape implements Drawable{public function __construct(private float $radius) {}public function area(): float{return pi() * $this-radius * $this-radius;}public function draw(): string{return 画一个半径为 {$this-radius} 的圆;}}class Rectangle extends Shape implements Drawable{public function __construct(private float $width, private float $height) {}public function area(): float{return $this-width * $this-height;}public function draw(): string{return 画一个 {$this-width}x{$this-height} 的矩形;}}?Trait的代码复用。phptrait Timestampable{private DateTime $createdAt;public function initializeTimestamps(): void{$this-createdAt new DateTime();}public function getCreatedAt(): DateTime{return $this-createdAt;}public function getAgeInDays(): int{return (int)$this-createdAt-diff(new DateTime())-days;}}trait SoftDeletable{private bool $deleted false;private ?DateTime $deletedAt null;public function softDelete(): void{$this-deleted true;$this-deletedAt new DateTime();}public function restore(): void{$this-deleted false;$this-deletedAt null;}public function isDeleted(): bool{return $this-deleted;}}class Post{use Timestampable, SoftDeletable;public function __construct(private string $title,private string $content) {$this-initializeTimestamps();}}$post new Post(标题, 内容);$post-softDelete();echo 是否删除: . ($post-isDeleted() ? 是 : 否) . \n;echo 是否可Trait: . (in_array(Timestampable::class, class_uses($post)) ? 是 : 否) . \n;?方法重写的规则。phpclass Base{public function publicMethod(): void {}protected function protectedMethod(): void {}private function privateMethod(): void {}final public function finalMethod(): void {}}class Child extends Base{// 可以重写public和protectedpublic function publicMethod(): void {}protected function protectedMethod(): void {}// private方法不是重写是新的方法private function privateMethod(): void {}// final方法不能重写// public function finalMethod(): void {} // 错误}?PHP的继承是单继承。Traits提供水平复用。接口支持多实现。理解final关键字和访问控制可以在设计类层次时做出更好的决策。
PHP继承与多态深入理解
发布时间:2026/6/7 9:45:24
PHP继承与多态深入理解继承和多态是面向对象编程的核心概念。PHP的继承机制和C、Java有些不同。今天说说PHP中的继承和多态。基本的类继承。phpclass Animal{public function __construct(protected string $name) {}public function speak(): string{return 动物叫声;}public function getName(): string{return $this-name;}}class Dog extends Animal{public function speak(): string{return 汪汪!;}}class Cat extends Animal{public function speak(): string{return 喵喵!;}}$animals [new Dog(旺财), new Cat(咪咪)];foreach ($animals as $animal) {echo {$animal-getName()}: {$animal-speak()}\n;}?抽象类和接口。phpabstract class Shape{abstract public function area(): float;public function description(): string{return 一个面积为 . $this-area() . 的形状;}}interface Drawable{public function draw(): string;}class Circle extends Shape implements Drawable{public function __construct(private float $radius) {}public function area(): float{return pi() * $this-radius * $this-radius;}public function draw(): string{return 画一个半径为 {$this-radius} 的圆;}}class Rectangle extends Shape implements Drawable{public function __construct(private float $width, private float $height) {}public function area(): float{return $this-width * $this-height;}public function draw(): string{return 画一个 {$this-width}x{$this-height} 的矩形;}}?Trait的代码复用。phptrait Timestampable{private DateTime $createdAt;public function initializeTimestamps(): void{$this-createdAt new DateTime();}public function getCreatedAt(): DateTime{return $this-createdAt;}public function getAgeInDays(): int{return (int)$this-createdAt-diff(new DateTime())-days;}}trait SoftDeletable{private bool $deleted false;private ?DateTime $deletedAt null;public function softDelete(): void{$this-deleted true;$this-deletedAt new DateTime();}public function restore(): void{$this-deleted false;$this-deletedAt null;}public function isDeleted(): bool{return $this-deleted;}}class Post{use Timestampable, SoftDeletable;public function __construct(private string $title,private string $content) {$this-initializeTimestamps();}}$post new Post(标题, 内容);$post-softDelete();echo 是否删除: . ($post-isDeleted() ? 是 : 否) . \n;echo 是否可Trait: . (in_array(Timestampable::class, class_uses($post)) ? 是 : 否) . \n;?方法重写的规则。phpclass Base{public function publicMethod(): void {}protected function protectedMethod(): void {}private function privateMethod(): void {}final public function finalMethod(): void {}}class Child extends Base{// 可以重写public和protectedpublic function publicMethod(): void {}protected function protectedMethod(): void {}// private方法不是重写是新的方法private function privateMethod(): void {}// final方法不能重写// public function finalMethod(): void {} // 错误}?PHP的继承是单继承。Traits提供水平复用。接口支持多实现。理解final关键字和访问控制可以在设计类层次时做出更好的决策。