PHP Classes Objects

Understanding Object-Oriented Programming (OOP) in PHP (了解PHP中的面向对象编程( OOP ))

Object-Oriented Programming (OOP) is a programming paradigm that is based on the concept of “objects”, which can contain data and code that operates on that data. OOP allows developers to create complex and dynamic web applications with reusable and modular code. In PHP, OOP is an essential part of the language and is widely used by developers to create scalable and maintainable applications. (面向对象编程( OOP )是一种基于“对象”概念的编程范式,它可以包含对数据进行操作的数据和代码。OOP允许开发人员使用可重用和模块化代码创建复杂和动态的Web应用程序。在PHP中, OOP是语言的重要组成部分,被开发人员广泛用于创建可扩展和可维护的应用程序。)

What are Classes and Objects in PHP OOP?

What are Classes and Objects in PHP OOP? (PHP OOP中的类和对象是什么?)

Classes are blueprint definitions for creating objects in PHP. Classes can contain properties (variables) and methods (functions). Objects are instances of classes, created using the new keyword. The properties of an object store its data and the methods operate on that data. Classes and objects are the building blocks of OOP in PHP. (类是在PHP中创建对象的蓝图定义。类可以包含属性(变量)和方法(函数)。对象是使用new关键字创建的类实例。对象的属性存储其数据,方法对该数据进行操作。类和对象是PHP中OOP的构建块。)

Defining Classes in PHP

Defining Classes in PHP (在PHP中定义类)

Classes in PHP are defined using the class keyword, followed by the name of the class. The properties and methods of a class are defined inside the class definition, within curly braces ({}). Here is an example of a class definition in PHP:

class User {
   public $username;
   public $email;
   public function __construct($username, $email) {
       $this->username = $username;
       $this->email = $email;
   }
   public function getUsername() {
       return $this->username;
   }
   public function getEmail() {
       return $this->email;
   }
}

In this example, the User class has two properties, $username and $email, and two methods, getUsername() and getEmail(). The construct method is a special method in PHP that is called when an object is created from a class. (在此示例中, User类有两个属性: $ username和$ email ,以及两个方法: getUsername ()和getEmail ()。 construct方法是PHP中的一个特殊方法,在从类创建对象时调用。)

Creating Objects from Classes

Creating Objects from Classes (从类创建对象)

To create an object from a class, use the new keyword, followed by the name of the class. Here is an example of how to create an object from the User class:

$user = new User("John Doe", "[email protected]");

This creates a new User object and stores it in the variable $user. You can access the properties and methods of the object using the arrow operator (->). Here is an example of how to access the properties and methods of the $user object:

echo $user->username; // Outputs: "John Doe"
echo $user->email; // Outputs: "[email protected]"
echo $user->getUsername(); // Outputs: "John Doe"
echo $user->getEmail(); // Outputs: "[email protected]"

Inheritance in PHP OOP

Inheritance in PHP OOP (PHP OOP中的继承)

Inheritance is a feature of OOP that allows classes to inherit properties and methods from parent classes. This allows developers to create new classes that are based on existing classes, without having to write all the code again. In PHP, inheritance is defined using the extends keyword. Here is an example of inheritance in PHP:

class Admin extends User {
   public $permissions;
   public function __construct($username, $email, $permissions) {
       parent::__construct($username, $email);
       $this->permissions = $permissions;
   }
   public function getPermissions() {
      return $this->permissions;
  }
}

In this example, the Admin class extends the User class and inherits its properties and methods. The Admin class also has its own property, $permissions, and method, getPermissions(). The parent:: keyword is used to call the parent class’s __construct method, allowing the Admin class to reuse the logic from the User class.

Polymorphism in PHP OOP

Polymorphism in PHP OOP (PHP OOP中的多态性)

Polymorphism is a feature of OOP that allows objects of different classes to be treated as objects of the same class. This allows developers to write generic code that can work with objects of different classes, as long as they have the same methods. In PHP, polymorphism is achieved by defining common methods in parent classes and implementing them in child classes. Here is an example of polymorphism in PHP:

<?php

class User
{
   public $username;
   public $email;

   public function __construct($username, $email)
(公共函数__ construct ($ username, $ email))
   {
       $this->username = $username;
       $this->email = $email;
   }
   public function getUsername()
(公共函数getUsername ())
   {
       return $this->username;
   }
   public function getEmail()
(公共函数getEmail ())
   {
       return $this->email;
   }

   public function showInfo()
(公共函数showInfo ())
   {
       echo "Username: " . $this->username . "\n";
       echo "Email: " . $this->email . "\n";
   }
}

class Admin extends User
{
   public $permissions;
   public function __construct($username, $email, $permissions)
(公共函数__ construct ($ username, $ email, $ permissions))
   {
       parent::__construct($username, $email);
       $this->permissions = $permissions;
   }
   public function getPermissions()
(公共函数getPermissions ())
   {
       return $this->permissions;
   }

   public function showInfo()
(公共函数showInfo ())
   {
       parent::showInfo();
       echo "Permissions: " . $this->permissions . "\n";
   }
}

$user = new User("John Doe", "[email protected]");
$admin = new Admin("Jane Doe", "[email protected]", ["read", "write", "delete"]);

$users = [$user, $admin];

foreach ($users as $user) {
   $user->showInfo();
}

In this example, the User class and the Admin class both have a showInfo() method. When the showInfo() method is called on an object, the correct implementation of the method is called, based on the type of the object. This allows the foreach loop to treat the $user and $admin objects as objects of the same type, even though they are instances of different classes. (在此示例中, “User”类和“Admin”类都具有“showInfo ()”方法。在对象上调用“showInfo ()”方法时,会根据对象的类型调用方法的正确实现。这允许foreach循环将$ user$ admin对象视为相同类型的对象,即使它们是不同类的实例。)

Conclusion

Conclusion (小结)

Object-Oriented Programming (OOP) is a powerful programming paradigm that is widely used in PHP for creating scalable and maintainable web applications. Classes and objects are the building blocks of OOP in PHP, and inheritance and polymorphism are two of the key features that allow developers to write efficient and reusable code. Whether you are just starting out with PHP or are a seasoned developer, understanding the basics of OOP in PHP is essential for creating high-quality web applications.



请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部