PHP 与 Java 一样,支持面向对象编程,并使用类和对象作为其核心构建块。理解这些概念对于掌握 PHP 至关重要。本指南将涵盖您需要了解的有关 PHP 中的类和对象的所有内容。
PHP 中的类是创建对象的蓝图。它定义了类的对象将具有的结构和行为。
class ClassName { // Properties (Fields) // Methods }
class Car { // Properties public $color; public $model; public $year; // Methods public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
对象是类的实例。您可以使用 new 关键字从类创建对象。
$objectName = new ClassName();
class Main { public function run() { $myCar = new Car(); // Creating an object of the Car class $myCar->color = "Red"; $myCar->model = "Tesla"; $myCar->year = 2022; $myCar->displayInfo(); } } $main = new Main(); $main->run();
属性(也称为字段)表示对象的状态,而方法定义对象的行为。
属性是保存对象数据的变量。
class Car { public $color; public $model; public $year; }
方法是在类中定义的函数,用于描述对象的行为。
class Car { public $color; public $model; public $year; public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
构造函数是实例化对象时自动调用的特殊方法。他们初始化新创建的对象。
如果没有定义构造函数,PHP 会提供一个不带参数的默认构造函数。
class Car { public $color; public $model; public $year; // Default constructor public function __construct() { } public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
参数化构造函数允许您使用特定值初始化对象。
class Car { public $color; public $model; public $year; // Parameterized constructor public function __construct($color, $model, $year) { $this->color = $color; $this->model = $model; $this->year = $year; } public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
class Main { public function run() { $myCar = new Car("Red", "Tesla", 2022); $myCar->displayInfo(); } } $main = new Main(); $main->run();
PHP 本身并不像 Java 那样支持方法重载,但您可以使用可选参数或通过在单个构造函数中手动处理参数来模拟它。
class Car { public $color; public $model; public $year; // Simulating constructor overloading public function __construct($color = "Unknown", $model = "Unknown", $year = 0) { $this->color = $color; $this->model = $model; $this->year = $year; } public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
class Main { public function run() { $defaultCar = new Car(); $defaultCar->displayInfo(); $myCar = new Car("Red", "Tesla", 2022); $myCar->displayInfo(); } } $main = new Main(); $main->run();
PHP 中的封装是将数据(属性)和方法(函数)捆绑在一个类中的做法。它确保对象的内部状态不受外界干扰和误用。
PHP 中的访问修饰符控制属性、方法和构造函数的可见性和可访问性。 PHP 支持三种主要的访问修饰符:
class ClassName { // Properties (Fields) // Methods }
PHP 中的静态成员与类本身相关联,而不是与任何特定实例相关联。无需创建类的对象即可访问它们。
静态属性在类的所有实例之间共享。它们是使用 static 关键字声明的。
class Car { // Properties public $color; public $model; public $year; // Methods public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
静态方法是使用 static 关键字声明的。它们属于类而不是实例。
$objectName = new ClassName();
静态成员是使用类名访问的,而不是通过对象。
class Main { public function run() { $myCar = new Car(); // Creating an object of the Car class $myCar->color = "Red"; $myCar->model = "Tesla"; $myCar->year = 2022; $myCar->displayInfo(); } } $main = new Main(); $main->run();
PHP 中的访问修饰符控制类成员的可见性,确保封装并强制执行访问限制。
class Car { public $color; public $model; public $year; }
class Car { public $color; public $model; public $year; public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
class Car { public $color; public $model; public $year; // Default constructor public function __construct() { } public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
PHP 中的非访问修饰符修改类成员的行为而不影响其可见性。
class ClassName { // Properties (Fields) // Methods }
class Car { // Properties public $color; public $model; public $year; // Methods public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
$objectName = new ClassName();
PHP 中的继承是一种机制,其中一个类(子类)可以继承另一个类(超类)的属性和方法。它促进代码重用并允许在类之间创建层次关系。
class Main { public function run() { $myCar = new Car(); // Creating an object of the Car class $myCar->color = "Red"; $myCar->model = "Tesla"; $myCar->year = 2022; $myCar->displayInfo(); } } $main = new Main(); $main->run();
class Car { public $color; public $model; public $year; }
在此示例中:
PHP 中的访问修饰符决定子类和程序其他部分中类成员的可见性。它们在继承中发挥着关键作用。
class Car { public $color; public $model; public $year; public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
PHP 中的静态成员与类相关联,而不是与任何特定实例相关联。它们遵循与继承中的非静态成员相同的访问规则。
class Car { public $color; public $model; public $year; // Default constructor public function __construct() { } public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
静态方法在 PHP 中是继承的,但不能像实例方法一样被重写。当子类定义同名静态方法时,会隐藏父类的静态方法。
class ClassName { // Properties (Fields) // Methods }
PHP 中的抽象方法必须定义在抽象类中。抽象方法在超类中的可见性决定了它在子类中的可见性。子类必须实现具有相同或较少限制的访问修饰符的抽象方法。
class Car { // Properties public $color; public $model; public $year; // Methods public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
PHP 中的 Final 方法不能被子类覆盖,并且 Final 类不能扩展。
$objectName = new ClassName();
在 PHP 中,顶级类(未嵌套在其他类中的类)的声明遵循特定的关键字顺序。声明可以包含访问修饰符、abstract 或final 关键字以及class 关键字。
class ClassName { // Properties (Fields) // Methods }
class Car { // Properties public $color; public $model; public $year; // Methods public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
$objectName = new ClassName();
class Main { public function run() { $myCar = new Car(); // Creating an object of the Car class $myCar->color = "Red"; $myCar->model = "Tesla"; $myCar->year = 2022; $myCar->displayInfo(); } } $main = new Main(); $main->run();
class Car { public $color; public $model; public $year; }
class Car { public $color; public $model; public $year; public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
class Car { public $color; public $model; public $year; // Default constructor public function __construct() { } public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
class Car { public $color; public $model; public $year; // Parameterized constructor public function __construct($color, $model, $year) { $this->color = $color; $this->model = $model; $this->year = $year; } public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
class Main { public function run() { $myCar = new Car("Red", "Tesla", 2022); $myCar->displayInfo(); } } $main = new Main(); $main->run();
PHP 中的抽象类与 Java 中的抽象类类似,用于定义其他类的蓝图。它们包含抽象方法(没有实现的方法)和具体方法(有实现的方法)。抽象类使用abstract关键字声明,不能直接实例化。
抽象类充当派生类的基类。它为其子类定义了常见行为,同时允许在这些子类中实现特定方法。
要在 PHP 中声明抽象类,请在 class 关键字之前使用abstract 关键字。
class ClassName { // Properties (Fields) // Methods }
抽象方法在抽象类中定义,但没有方法体。子类必须实现所有抽象方法。
class Car { // Properties public $color; public $model; public $year; // Methods public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
抽象类中的具体方法具有主体,可以由子类按原样继承或重写。
$objectName = new ClassName();
抽象类的子类必须实现其所有抽象方法,除非子类也声明为抽象。
class Main { public function run() { $myCar = new Car(); // Creating an object of the Car class $myCar->color = "Red"; $myCar->model = "Tesla"; $myCar->year = 2022; $myCar->displayInfo(); } } $main = new Main(); $main->run();
抽象类不能直接实例化。您必须使用具体的子类来创建实例。
class Car { public $color; public $model; public $year; }
抽象类可以有构造函数,它们的构造函数在子类实例化时被调用。
class Car { public $color; public $model; public $year; public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
抽象类可以包含实例变量和具体方法,为子类提供可重用的功能。
class Car { public $color; public $model; public $year; // Default constructor public function __construct() { } public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
抽象类可以包含静态方法。静态方法属于类,无需实例化即可调用。
class ClassName { // Properties (Fields) // Methods }
class Car { // Properties public $color; public $model; public $year; // Methods public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
$objectName = new ClassName();
class Main { public function run() { $myCar = new Car(); // Creating an object of the Car class $myCar->color = "Red"; $myCar->model = "Tesla"; $myCar->year = 2022; $myCar->displayInfo(); } } $main = new Main(); $main->run();
PHP 中的接口为实现它的类定义了一个契约。它指定类必须实现的方法,但本身不提供任何方法实现。接口允许更灵活和模块化的代码,使类能够遵守一组通用的方法签名,无论其继承层次结构如何。
PHP中的接口类似于抽象类,但它只能定义方法签名而没有任何实现。实现接口的类必须提供接口中声明的所有方法的实现。一个类可以实现多个接口,这使得接口成为 PHP 支持行为多重继承的关键部分。
要声明接口,请使用interface关键字。接口只能包含方法声明(没有方法体)、常量和抽象方法。
class Car { public $color; public $model; public $year; }
实现接口的类必须提供接口中声明的所有方法的实现。一个类可以实现多个接口,接口之间用逗号分隔。
class Car { public $color; public $model; public $year; public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
接口中的方法不能有主体,并且它们必须是公共的。当类实现接口时,它必须完全按照接口中声明的方式实现这些方法,包括方法签名。
class Car { public $color; public $model; public $year; // Default constructor public function __construct() { } public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
PHP中的一个类可以实现多个接口。这使得设计系统时更加灵活,其中类可以遵守不同的契约。
class ClassName { // Properties (Fields) // Methods }
接口可以包含常量。这些常量自动是公共的,并且可以被任何实现该接口的类访问。
class Car { // Properties public $color; public $model; public $year; // Methods public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
一个接口可以扩展另一个接口。这意味着子接口继承了父接口的所有方法(签名),并且还可以添加新的方法。实现子接口的类必须实现父接口和子接口的所有方法。
$objectName = new ClassName();
接口不能包含静态方法。接口中声明的所有方法都必须是实例方法。接口中不允许使用静态方法,因为接口为实现类定义了实例级契约。
class Main { public function run() { $myCar = new Car(); // Creating an object of the Car class $myCar->color = "Red"; $myCar->model = "Tesla"; $myCar->year = 2022; $myCar->displayInfo(); } } $main = new Main(); $main->run();
class Car { public $color; public $model; public $year; }
class Car { public $color; public $model; public $year; public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
以上是PHP、类和对象的详细内容。更多信息请关注PHP中文网其他相关文章!