PHP 面向对象编程(OOP)
1. 什么是 PHP 面向对象编程(OOP)?
PHP 面向对象编程(OOP) 是一种基于 类(Class)和对象(Object) 的编程范式,强调 封装(Encapsulation)、继承(Inheritance)和多态(Polymorphism),使代码更加模块化、可复用和易维护。
✅ OOP 的优势:
- 代码可重用:通过类和继承机制减少重复代码。
- 代码更易管理:使用类结构组织代码,提高可读性。
- 更高的安全性:可以使用访问控制(
public、private、protected)。 - 更强的扩展性:使用继承和多态机制扩展已有功能。
2. PHP 类和对象
定义类(Class)
<?php
class Car {
// 成员变量(属性)
public $brand;
public $color;
// 构造函数
function __construct($brand, $color) {
$this->brand = $brand;
$this->color = $color;
}
// 成员方法(函数)
function displayInfo() {
return "This car is a $this->color $this->brand.";
}
}
?>
创建对象
<?php
$car1 = new Car("Toyota", "Red");
echo $car1->displayInfo(); // 输出: This car is a Red Toyota.
?>
✅ 解释
new关键字用于创建对象。$this->propertyName访问当前对象的属性。__construct()是构造函数,每次创建对象时自动执行。
3. PHP 访问控制修饰符
PHP 提供 public、protected 和 private 三种访问修饰符:
| 关键字 | 作用 |
|---|---|
public | 公开访问,类内外部都可以访问 |
protected | 受保护,只有类内部和子类可以访问 |
private | 私有,只有类内部可以访问 |
访问控制示例
<?php
class Person {
public $name; // 公开
protected $age; // 受保护
private $password; // 私有
function __construct($name, $age, $password) {
$this->name = $name;
$this->age = $age;
$this->password = $password;
}
function getAge() {
return $this->age; // 受保护的属性可以在类内访问
}
}
$person = new Person("Alice", 25, "1234");
echo $person->name; // ✅ 允许访问
// echo $person->age; // ❌ 报错:protected
// echo $person->password; // ❌ 报错:private
echo $person->getAge(); // ✅ 通过方法访问 protected 属性
?>
4. PHP 继承(Inheritance)
子类可以继承父类的属性和方法,从而减少代码重复。
📌 定义子类
<?php
class Animal {
public $name;
function __construct($name) {
$this->name = $name;
}
function makeSound() {
return "Some sound";
}
}
// Dog 继承 Animal
class Dog extends Animal {
function makeSound() {
return "Woof! Woof!";
}
}
$dog = new Dog("Buddy");
echo $dog->name; // ✅ 继承父类属性
echo $dog->makeSound(); // 输出: Woof! Woof!
?>
✅ 说明
class Dog extends Animal:Dog继承Animal。- 子类
Dog可以重写(override) 父类方法。
5. PHP 多态(Polymorphism)
多态 允许子类以不同方式实现父类的同一方法。
多态示例
<?php
class Animal {
function makeSound() {
return "Some sound";
}
}
class Dog extends Animal {
function makeSound() {
return "Bark!";
}
}
class Cat extends Animal {
function makeSound() {
return "Meow!";
}
}
$animals = [new Dog(), new Cat()];
foreach ($animals as $animal) {
echo $animal->makeSound() . "\n";
}
?>
✅ 说明
Dog和Cat继承Animal,但实现自己的makeSound()方法。foreach遍历对象数组,不管是Dog还是Cat,都可以调用makeSound()。
6. PHP 抽象类(Abstract Class)
抽象类 不能实例化,只能被继承,并且可以包含抽象方法(不带方法体)。
抽象类示例
<?php
abstract class Shape {
abstract function getArea();
}
class Circle extends Shape {
private $radius;
function __construct($radius) {
$this->radius = $radius;
}
function getArea() {
return pi() * $this->radius * $this->radius;
}
}
$circle = new Circle(5);
echo $circle->getArea(); // 输出: 78.5398
?>
✅ 说明
abstract class Shape不能直接实例化。abstract function getArea();在子类Circle必须实现。
7. PHP 接口(Interface)
接口 规定了必须实现的方法,但不包含实现。
接口示例
<?php
interface Animal {
function makeSound();
}
class Dog implements Animal {
function makeSound() {
return "Bark!";
}
}
class Cat implements Animal {
function makeSound() {
return "Meow!";
}
}
$dog = new Dog();
echo $dog->makeSound(); // 输出: Bark!
?>
✅ 说明
interface Animal只规定makeSound()方法,不提供实现。class Dog implements Animal必须实现makeSound()。
8. PHP 静态方法 & 属性
静态方法
<?php
class MathUtils {
public static function add($a, $b) {
return $a + $b;
}
}
echo MathUtils::add(3, 5); // 输出: 8
?>
✅ 说明
static方法无需创建对象,直接调用:ClassName::methodName()。
静态属性
<?php
class Counter {
public static $count = 0;
static function increment() {
self::$count++;
}
}
Counter::increment();
echo Counter::$count; // 输出: 1
?>
✅ 说明
static属性通过self::$propertyName访问。
10. 总结
| 特性 | 描述 |
|---|---|
| 类 | class MyClass {} |
| 对象 | $obj = new MyClass(); |
| 构造函数 | function __construct() {} |
| 访问控制 | public、protected、private |
| 继承 | class Child extends Parent {} |
| 多态 | 子类可重写父类方法 |
| 抽象类 | abstract class MyClass {} |
| 接口 | interface MyInterface {} |
| 静态方法 | public static function method() {} |
| 静态属性 | public static $var; |
🔗 推荐阅读
更多详细内容请关注其他相关文章!