类和对象
1. 概述
在面向对象编程(OOP)中,类和对象是两个核心概念。类是对象的蓝图或模板,而对象是类的实例。通过类,我们可以定义对象的属性和方法,从而实现代码的模块化和重用。
2. 类的定义
在 PHP 中,类通过 class 关键字定义。类可以包含属性(变量)和方法(函数)。
2.1 基本语法
class Car {
// 属性
public $brand;
public $model;
public $year;
// 构造函数
public function __construct($brand, $model, $year) {
$this->brand = $brand;
$this->model = $model;
$this->year = $year;
}
// 方法
public function getInfo() {
return "This is a {$this->year} {$this->brand} {$this->model}.";
}
}
2.2 构造函数
构造函数是一个特殊的方法,它在创建对象时自动调用。通常用于初始化对象的属性。
public function __construct($brand, $model, $year) {
$this->brand = $brand;
$this->model = $model;
$this->year = $year;
}
3. 对象的创建
通过 new 关键字可以创建类的实例(对象)。
$myCar = new Car("Toyota", "Corolla", 2020);
echo $myCar->getInfo(); // 输出: This is a 2020 Toyota Corolla.
4. 访问属性和方法
对象的属性和方法可以通过 -> 操作符访问。
echo $myCar->brand; // 输出: Toyota
echo $myCar->getInfo(); // 输出: This is a 2020 Toyota Corolla.
5. 实践练习
5.1 创建一个简单的类
创建一个名为 Book 的类,包含 title、author 和 year 属性,以及一个 getInfo 方法,返回书籍的信息。
class Book {
public $title;
public $author;
public $year;
public function __construct($title, $author, $year) {
$this->title = $title;
$this->author = $author;
$this->year = $year;
}
public function getInfo() {
return "{$this->title} by {$this->author}, published in {$this->year}.";
}
}
$myBook = new Book("1984", "George Orwell", 1949);
echo $myBook->getInfo(); // 输出: 1984 by George Orwell, published in 1949.
5.2 扩展类
创建一个名为 EBook 的类,继承自 Book 类,并添加一个 format 属性,表示电子书的格式(如 PDF、EPUB 等)。
class EBook extends Book {
public $format;
public function __construct($title, $author, $year, $format) {
parent::__construct($title, $author, $year);
$this->format = $format;
}
public function getInfo() {
return parent::getInfo() . " Format: {$this->format}.";
}
}
$myEBook = new EBook("1984", "George Orwell", 1949, "PDF");
echo $myEBook->getInfo(); // 输出: 1984 by George Orwell, published in 1949. Format: PDF.
6. 总结
通过本教程,我们学习了如何在 PHP 中定义类和创建对象,以及如何访问对象的属性和方法。类和对象是面向对象编程的基础,掌握它们对于编写模块化、可维护的代码至关重要。
7. 下一步
接下来,我们将学习如何使用继承和多态来进一步扩展和重用代码。