模式切换
面向对象编程
类和对象
定义类:
在 PHP 中,类使用 class
关键字定义,类可以包含属性和方法。
php
class Car {
public $brand; // 属性
public $color;
// 方法
public function drive() {
echo "The car is driving.";
}
}
创建对象:
使用 new
关键字实例化一个类对象。
php
$myCar = new Car(); // 创建对象
$myCar->brand = "Toyota"; // 设置属性
$myCar->color = "Red";
$myCar->drive(); // 调用方法
类的属性和方法:
- 属性:对象的特性,通过
public
、protected
或private
定义。 - 方法:对象的行为或功能,通过类中的函数定义。
构造函数与析构函数
构造函数:
使用 __construct()
方法,在创建对象时自动调用,用于初始化属性。
php
class Car {
public $brand;
public $color;
// 构造函数
public function __construct($brand, $color) {
$this->brand = $brand;
$this->color = $color;
}
public function displayInfo() {
echo "Brand: $this->brand, Color: $this->color";
}
}
$myCar = new Car("Honda", "Blue"); // 创建对象时传递参数
$myCar->displayInfo(); // 输出: Brand: Honda, Color: Blue
析构函数:
使用 __destruct()
方法,在对象销毁时自动调用,用于清理资源。
php
class Car {
public function __destruct() {
echo "The car is being destroyed.";
}
}
$myCar = new Car(); // 创建对象
// 当脚本执行结束或对象销毁时,会自动调用析构函数
访问控制
public:可以在任何地方访问(类内、类外、子类)。
protected:只能在类内部和子类中访问。
private:只能在类内部访问。
phpclass Car { public $brand; // 公开属性 protected $color; // 受保护属性 private $engineNumber; // 私有属性 public function setEngineNumber($number) { $this->engineNumber = $number; } private function startEngine() { echo "Engine started."; } } $myCar = new Car(); $myCar->brand = "Toyota"; // 可以访问 // $myCar->color = "Red"; // 错误,不能直接访问受保护属性 // $myCar->engineNumber = "12345"; // 错误,不能直接访问私有属性
继承与多态
继承:
- 子类可以继承父类的属性和方法,并且可以扩展或修改。
- 使用
extends
关键字表示继承关系。
php
class Vehicle {
public $type;
public function setType($type) {
$this->type = $type;
}
}
class Car extends Vehicle {
public $brand;
public function setBrand($brand) {
$this->brand = $brand;
}
}
$myCar = new Car();
$myCar->setType("SUV");
$myCar->setBrand("Honda");
方法重写:
子类可以重写父类的方法,提供不同的实现。
php
class Animal {
public function makeSound() {
echo "Some generic animal sound.";
}
}
class Dog extends Animal {
public function makeSound() {
echo "Bark!";
}
}
$myDog = new Dog();
$myDog->makeSound(); // 输出: Bark!
接口与抽象类
接口:
- 使用
interface
关键字定义,接口中的所有方法都必须由实现类来定义。 - 类可以实现多个接口。
php
interface Drivable {
public function drive();
}
interface Maintainable {
public function maintain();
}
class Car implements Drivable, Maintainable {
public function drive() {
echo "The car is driving.";
}
public function maintain() {
echo "The car is being maintained.";
}
}
抽象类:
- 使用
abstract
关键字定义,不能实例化。 - 可以包含抽象方法和具体方法,子类必须实现抽象方法。
php
abstract class Animal {
abstract public function makeSound();
public function eat() {
echo "The animal is eating.";
}
}
class Cat extends Animal {
public function makeSound() {
echo "Meow!";
}
}
$myCat = new Cat();
$myCat->makeSound(); // 输出: Meow!
$myCat->eat(); // 输出: The animal is eating.
静态方法与属性
静态属性和方法:
使用 static
关键字定义,属于类而不是对象,可以直接通过类名访问。
php
class Math {
public static $pi = 3.14159;
public static function square($number) {
return $number * $number;
}
}
echo Math::$pi; // 输出: 3.14159
echo Math::square(4); // 输出: 16
命名空间与自动加载
命名空间:
使用 namespace
关键字定义,可以避免类名冲突。
php
namespace MyApp;
class User {
public function getName() {
return "Alice";
}
}
自动加载:
使用 spl_autoload_register()
可以自动加载类文件,避免手动引入文件。
php
spl_autoload_register(function ($className) {
include $className . ".php";
});
$user = new MyApp\User();
echo $user->getName();