概述
PHP PHP 中的面向对象编程 (OOP) PHP OOP 可以说是一种基于对象概念的编程模型,对象包含属性或属性形式的数据,以及方法,它们是可以在对象上执行的函数。PHP 是一种流行的服务器端脚本语言,支持 OOP。在PHP中,类用于定义对象,可以使用“new”关键字从类创建对象。封装、继承和多态性是 PHP 支持的 OOP 的关键概念。
什么是OOP?
PHP OOP,即面向对象编程,是 PHP 中使用的一种编程范式,其中对象是从包含属性形式的数据和方法形式的函数的类创建的。OOP 基于封装、继承和多态性的概念。封装提供数据隐藏和抽象,允许更安全、更灵活的代码。继承允许创建新类,这些类可以从现有类继承属性和方法,从而节省代码开发的时间和精力。
PHP中面向对象的概念和原则
以下是 PHP OOP 的一些关键概念和原则:
- 封装:这是将数据和行为捆绑在对象中并将细节隐藏给外界的概念。在 PHP 中,这通常是通过使用访问修饰符(如 public、private 和 protected)来实现的。
- 继承:这是从现有类创建新类、继承属性和方法的能力。在 PHP 中,继承是使用 “extends” 关键字实现的。
- 多态性:这是创建具有相同名称的多个方法的能力,这些方法的行为因上下文而异。在PHP中,多态性通常是通过使用接口和抽象类来实现的。
- 抽象:这是为复杂功能定义简化接口的概念。在 PHP 中,这可以通过使用接口、抽象类和特征来实现。
什么是类和对象?
类和对象是 PHP OOP 的关键组件。类是用于创建对象的蓝图或模板,而对象是类的实例。
在PHP中,使用“class”关键字定义类,后跟类名和包含属性(数据)和方法(函数)的代码块。例如:
class Car {
public $color;
public function StartEngine() {
echo "The engine has started.";
}
}
解释
这定义了一个名为“Car”的类,该类具有一个名为“color”的公共属性和一个名为“StartEngine”的公共方法。
若要从此类创建对象,请使用“new”关键字,后跟类名和括号。例如:
$myCar = new Car();
这将从“Car”类创建一个名为“$myCar”的新对象。
在 PHP 中创建对象
PHP 中的对象是使用“new”关键字后跟类名创建的。
下面是在 PHP 中创建对象的示例:
// Define a class
class Person {
public $name;
public $age;
}
// Create an object
$person = new Person();
// Set the properties of the object
$person->name = "Yash Agarwal";
$person->age = 21;
// Access the properties of the object
echo $person->name; // Output: Yash Agarwal
echo $person->age; // Output: 21
解释
在PHP中,我们还可以在创建对象时将参数传递给类的构造函数。构造函数是在创建对象时调用的特殊方法,用于初始化对象的属性。
下面是在 PHP 中创建带有参数的对象的示例:
// Define a class with a constructor
class Person {
public $name;
public $age;
public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
}
// Create an object with arguments
$person = new Person("Yash Agarwal", 21);
// Access the properties of the object
echo $person->name; // Output: Yash Agarwal
echo $person->age; // Output:21
解释
在此示例中,我们定义一个名为“Person”的类,其构造函数采用两个参数:“name”和“age”。我们使用“this”关键字根据传递给构造函数的参数来设置对象属性的值。
在 PHP 中调用成员函数
在PHP中,成员函数(也称为方法)是在类中定义的函数,可以在该类的对象上调用。下面是在 PHP 中定义和调用成员函数的示例:
class Person {
public $name;
public $age;
public function sayHello() {
echo "Hello, my name is ". $this->name . " and I am " $this->age . " years old.";
}
}
// Create an object of the Person class
$person = new Person();
$person->name = "John Doe";
$person->age = 30;
// Call the sayHello() method on the object
$person->sayHello(); // Output: Hello, my name is John Doe and I am 30 years old.
构造函数
在PHP OOP中,构造函数是一种特殊方法,在创建对象时自动调用。构造函数用于初始化对象的属性并设置其初始状态。
要在 PHP 中定义构造函数,请使用 “__construct()” 方法。以下是在 PHP 中定义和使用构造函数的示例:
class Person {
public $name;
public $age;
public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
public function sayHello() {
echo "Hello, my name is " $this->name . " and I am " $this->age . " years old.";
}
}
// Create an object of the Person class using the constructor
$person = new Person("Yash Agarwal", 21);
// Call the sayHello() method on the object
$person->sayHello(); // Output: Hello, my name is Yash Agarwal and I am 21 years old.
解释
在此示例中,我们定义了一个名为“Person”的类,该类具有两个公共属性:“name”和“age”,以及一个名为“__construct()”的构造函数,该函数采用两个参数:“name”和“age”。在构造函数中,我们使用“this”关键字设置对象属性的值。
破坏者
在PHP OOP中,析构函数是一种特殊的方法,当一个对象不再需要并且即将被销毁时,它会自动调用。析构函数用于在从内存中删除对象之前执行清理任务。
要在 PHP 中定义析构函数,请使用 “__destruct()” 方法。以下是在 PHP 中定义和使用析构函数的示例:
class Person {
public $name;
public $age;
public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
public function sayHello() {
echo "Hello, my name is " . $this->name . " and I am " . $this->age . " years old.";
}
public function __destruct() {
echo "Goodbye, " . $this->name . "!";
}
}
// Create an object of the Person class using the constructor
$person = new Person("Yash Agarwal", 21);
// Call the sayHello() method on the object
$person->sayHello(); // Output: Hello, my name is Yash Agarwal and I am 21 years old.
// Destroy the object
unset($person); // Output: Goodbye, Yash Agarwal!
解释在此示例中,我们定义了一个名为“Person”的类,该类具有两个公共属性:“name”和“age”,一个初始化属性的构造函数,以及一个名为“__destruct()”的析构函数,用于将消息打印到控制台。
然后,我们使用构造函数创建一个 “Person” 类的对象,并调用 “sayHello()” 方法将消息打印到控制台。
最后,我们使用“unset()”函数销毁对象,该函数调用析构函数并将消息打印到控制台。
遗产
PHP OOP 中的继承是 PHP OOP 中的一个重要概念,它允许您基于现有类创建新类。在 PHP 中,“extends”关键字支持继承,它允许新类继承现有类的属性和方法。
若要创建从现有类继承的类,请使用“class”关键字定义一个新类,并包含要使用“extends”关键字继承的类的名称。下面是一个示例:
class Animal {
public $name;
public function makeSound() {
echo "Animal is making a sound.";
}
}
class Dog extends Animal {
public function makeSound() {
echo "Dog is barking.";
}
}
// Create an object of the Dog class
$dog = new Dog();
$dog->name = "Fido";
// Call the makeSound() method on the Dog object
$dog->makeSound(); // Output: Dog is barking.
解释在此示例中,我们定义了一个名为“Animal”的基类,其中包含一个名为“name”的公共属性和一个名为“makeSound()”的公共方法。然后,我们创建一个名为“Dog”的新类,该类使用名为“extends”的关键字扩展“Animal”类。我们可以在上面的例子中看到的 “Dog” 类覆盖了 “Animal” 类中的 “makeSound()” 方法,使狗吠叫。
函数覆盖
PHP OOP 中的函数重写是面向对象编程中的一个概念,它允许子类提供已在其父类中定义的方法的不同实现。在 PHP 中,函数覆盖是通过在子类中定义与父类中的方法具有相同名称和参数的方法来实现的。
下面是 PHP 中函数覆盖的示例
class Animal {
public function makeSound() {
echo "The animal makes a sound.";
}
}
class Dog extends Animal {
public function makeSound() {
echo "The dog barks.";
}
}
// Create an object of the Dog class
$dog = new Dog();
// Call the makeSound() method on the Dog object
$dog->makeSound(); // Output: The dog barks.
在此示例中,我们有一个名为“Animal”的父类,其中包含一个名为 makeSound() 的方法。然后,我们定义一个名为“Dog”的子类,它使用关键字 extends 扩展了 “Animal” 类,然后它用不同的实现覆盖了 makeSound() 方法。
当我们创建一个 “Dog” 类的对象并在其上调用 makeSound() 方法时,将执行该方法的子类实现,而不是父类实现。在编辑器中运行上述代码以获得更好的解释。
公共成员
在 PHP 中,公共成员是可以从类外部访问的类的属性或方法。这意味着任何有权访问类实例的代码都可以读取或修改公共成员。
要在 PHP 中定义 public 成员,只需使用 “public” 关键字声明它。下面是具有公共成员的类的示例:
class Person {
public $name;
public $age;
public function sayHello() {
echo "Hello, my name is " . $this->name . " and I am " . $this->age . " years old.";
}
}
// Create an object of the Person class
$person = new Person();
$person->name = "John Doe";
$person->age = 30;
// Call the sayHello() method on the object
$person->sayHello(); // Output: Hello, my name is John Doe and I am 30 years old.
解释在此示例中,我们定义了一个名为“Person”的类,该类具有两个公共属性:“name”和“age”,以及一个名为 sayHello() 的公共方法。sayHello() 方法使用“this”关键字访问对象的属性,并将消息打印到控制台。
私人会员
PHP OOP 中的私有成员是属性和方法,只能在定义它们的类中访问。这意味着无法从类外部访问或修改它们。
若要在 PHP 中定义私有成员,请在属性或方法名称之前使用“private”关键字。下面是在 PHP 中定义和使用私有成员的示例:
class Person {
private $name;
private $age;
public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
private function getAge() {
return $this->age;
}
public function sayHello() {
echo "Hello, my name is " . $this->name . " and I am " . $this->getAge() . " years old.";
}
}
// Create an object of the Person class using the constructor
$person = new Person("Yash Agarwal", 21);
// Call the sayHello() method on the object
$person->sayHello(); // Output: Hello, my name is Yash Agarwal and I am 21 years old.
受保护的成员
PHP OOP 中的受保护成员是类成员(属性和方法),只能在类本身及其子类中访问。
要在 PHP 中定义受保护的成员,请在成员名称之前使用“protected”关键字。以下是在 PHP 中定义和使用受保护成员的示例:
class Person {
protected $name;
protected $age;
public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
protected function getAge() {
return $this->age;
}
}
class Employee extends Person {
private $salary;
public function __construct($name, $age, $salary) {
parent::__construct($name, $age);
$this->salary = $salary;
}
public function getSalary() {
return $this->salary;
}
public function getAge() {
// Call the parent's getAge() method to access the protected member
return parent::getAge();
}
}
// Create an object of the Employee class
$employee = new Employee("Yash Agarwal", 21, 50000);
// Call the getAge() method on the object (inherited from the parent class)
echo $employee->getAge(); // Output: 21
// Call the getSalary() method on the object (defined in the subclass)
echo $employee->getSalary(); // Output: 50000
解释在此示例中,我们定义了一个名为“Person”的类,该类具有两个受保护的属性:“name”和“age”,以及一个名为getAge()的受保护方法。然后,我们定义一个名为“Employee”的子类,该子类继承自“Person”,并添加了一个名为“salary”的私有属性和一个名为getSalary()的公共方法。
接口
PHP OOP 中的接口是抽象方法的集合,用于定义类应实现的一组行为。接口为要遵循的类提供了蓝图,从而提供了更大的代码灵活性和可重用性。以下是在 PHP 中定义和使用接口的示例:
interface Shape {
public function getArea();
public function getPerimeter();
}
class Circle implements Shape {
private $radius;
public function __construct($radius) {
$this->radius = $radius;
}
public function getArea() {
return pi() * pow($this->radius, 2);
}
public function getPerimeter() {
return 2 * pi() * $this->radius;
}
}
class Rectangle implements Shape {
private $width;
private $height;
public function __construct($width, $height) {
$this->width = $width;
$this->height = $height;
}
public function getArea() {
return $this->width * $this->height;
}
public function getPerimeter() {
return 2 * ($this->width + $this->height);
}
}
// Create objects of the Circle and Rectangle classes
$circle = new Circle(5);
$rectangle = new Rectangle(3, 4);
// Call the getArea() and getPerimeter() methods on the objects
echo "Circle area: " . $circle->getArea() . "\n";
echo "Circle perimeter: " . $circle->getPerimeter() . "\n";
echo "Rectangle area: " . $rectangle->getArea() . "\n";
echo "Rectangle perimeter: " . $rectangle->getPerimeter() . "\n";
解释在此示例中,我们定义了一个名为“Shape”的接口,其中包含两个抽象方法:getArea() 和 getPerimeter()。然后,我们定义了两个类,Circle 和 “Rectangle”,它们实现了 “Shape” 接口,并提供了它们自己的 getArea() 和 getPerimeter() 方法的实现。
常数
在PHP中,常量是在脚本执行过程中无法更改的值。当需要定义将在整个代码中使用但不应更改的值时,常量非常有用。
要在 PHP 中定义常量,请使用 “define()” 函数,该函数采用两个参数:常量的名称及其值。以下是在 PHP 中定义和使用常量的示例:
// Define a constant
define("PI", 3.14159);
// Use the constant in a calculation
$radius = 5;
$area = PI * $radius * $radius;
// Print the result
echo "The area of the circle is: " . $area; // Output: The area of the circle is: 78.53975
解释在此示例中,我们使用 define() 函数定义一个名为“PI”的常量,其值为 3.14159。然后,我们在计算中使用常数来计算半径为 5 的圆的面积。最后,我们将结果打印到控制台。
抽象类
在PHP中,抽象类是一个不能直接实例化的类,而是由子类扩展的类。抽象类充当其他类继承的模板,提供可在多个类之间重用的通用功能。
要在 PHP 中定义抽象类,请在类声明中使用 “abstract” 关键字。下面是在 PHP 中定义和使用抽象类的示例:
abstract class Shape {
protected $color;
public function __construct($color) {
$this->color = $color;
}
abstract public function getArea();
}
class Square extends Shape {
protected $length;
public function __construct($color, $length) {
parent::__construct($color);
$this->length = $length;
}
public function getArea() {
return pow($this->length, 2);
}
}
// Create an object of the Square class
$square = new Square("red", 5);
// Call the getArea() method on the object
echo $square->getArea(); // Output: 25
解释在此示例中,我们定义了一个名为“Shape”的抽象类,该类具有受保护的属性“color”和抽象方法getArea()。getArea() 方法不是在抽象类中实现的,而是在子类中定义的。
静态关键字
在 PHP 中,“static”关键字用于声明无需创建类对象即可访问的类属性和方法。相反,它们可以直接从类本身访问。
下面是如何在 PHP 中使用 “static” 关键字的示例:
class MyClass {
public static $myProperty = "Hello, world!";
public static function myMethod() {
echo "This is a static method!";
}
}
// Access the static property
echo MyClass::$myProperty; // Output: Hello, world!
// Call the static method
MyClass::myMethod(); // Output: This is a static method!
解释在此示例中,我们定义了一个名为“MyClass”的类,该类具有一个名为“$myProperty”的静态属性和一个名为“myMethod()”的静态方法。我们可以使用类名后跟范围解析运算符 “::” 来访问静态属性。同样,我们可以使用相同的语法调用静态方法。
静态属性和方法在需要跨类的多个实例访问共享数据或功能的方案中非常有用。它们也常用于不需要任何状态的实用程序类,只需提供一组函数或常量。
最终关键字
在 PHP 中,final 关键字用于限制类、方法或属性的行为。当类、方法或属性标记为 final 时,任何子类都不能扩展或重写它。
final 关键字可以应用于类声明、方法声明或属性声明。以下是一些示例:
1. 最终班级声明:
final class MyClass {
// Class implementation
}
在上面的示例中,MyClass 类被标记为 final,这意味着没有其他类可以扩展它。
2. 最终方法声明:
class MyClass {
final public function myMethod() {
// Method implementation
}
}
在上面的示例中,myMethod() 方法被标记为 final,这意味着任何子类都无法覆盖它。
3. 最终财产声明:
class MyClass {
final public $myProperty = 'foo';
}
解释
在上面的示例中,$myProperty 属性被标记为 final,这意味着任何子类都不能重新声明或修改其值。
在希望确保不会以意外方式修改或扩展某个类、方法或属性的情况下,final 关键字可能很有用。但是,应谨慎使用它,因为它会限制代码的灵活性和可扩展性。
调用父构造函数
在 PHP 中,您可以使用 parent::__construct() 方法调用父类的构造函数。当您有一个子类需要执行一些基于父构造函数的工作构建的其他初始化时,这很有用。
下面是如何从子类调用父构造函数的示例:
class ParentClass {
public function __construct() {
// Parent constructor logic here
}
}
class ChildClass extends ParentClass {
public function __construct() {
parent::__construct(); // Call parent constructor
// Child constructor logic here
}
}
解释在上面的示例中,ChildClass 扩展了 ParentClass,并使用 parent::__construct() 调用父构造函数。这可确保父构造函数在子构造函数之前执行。
结论
- OOP 是一种编程范式,它强调使用对象来表示现实世界的实体,而不仅仅是函数和数据。
- 在 PHP 中,您可以定义类来创建对象,这些对象封装了数据和功能。
- 类可以具有属性(存储对象数据的变量)和方法(执行对象操作的函数)。
- 继承是 PHP 中 OOP 的一个关键特性,它允许子类从父类继承属性和方法。