Home >Backend Development >PHP Tutorial >How to use object data type in PHP

How to use object data type in PHP

王林
王林Original
2023-07-15 11:30:031476browse

How to use object data types in PHP

In PHP, an object is a special data type that can be used to represent entities or abstract concepts in the real world. Objects are created through classes, which define the properties and methods that the object has. In this article, we will learn how to use object data types in PHP and go through code examples to deepen our understanding.

Create a class
In PHP, we can use the class keyword to create a class. A class can contain multiple properties and methods. The following is a simple example:

class Person {
  public $name;
  public $age;
  
  public function sayHello() {
    echo "Hello, my name is " . $this->name . ".";
    echo " I am " . $this->age . " years old.";
  }
}

In this example, we create a class named Person, which has two public properties $name and $age, and a public method sayHello. Properties can be used to store the state of an object, while methods are used to define the behavior of the object.

Create an object
To create an object, we need to use the new keyword, followed by the name of the class, and parameters can be passed using parentheses. The following is an example of creating a Person object:

$person = new Person();

After we create an object, we can use the arrow-> syntax to access the properties and methods of the object. For example, we can assign values ​​to the $name and $age attributes and call the sayHello method:

$person->name = "John";
$person->age = 30;
$person->sayHello(); // 输出:Hello, my name is John. I am 30 years old.

Inheritance
PHP supports class inheritance. Through inheritance, we can create a new class that is derived from a Existing classes inherit properties and methods, and you can add your own properties and methods. For example, we can create a Student class that inherits from the Person class:

class Student extends Person {
  public $grade;
  
  public function sayGrade() {
    echo "I am in grade " . $this->grade . ".";
  }
}

In this example, the Student class inherits all the properties and methods of the Person class, and adds a new property $grade and a new Method sayGrade.

Create a Student object and call its methods:

$student = new Student();
$student->name = "Jane";
$student->age = 18;
$student->grade = 12;

$student->sayHello(); // 输出:Hello, my name is Jane. I am 18 years old.
$student->sayGrade(); // 输出:I am in grade 12.

Encapsulation and access control
Encapsulation in PHP is a mechanism to protect object properties and methods. We can use access Control modifiers to restrict access to properties and methods. Three access control modifiers are available: public, protected, and private.

  • public: Public access rights, properties and methods can be accessed inside and outside the class.
  • protected: Protected access, properties and methods can be accessed inside the class and in subclasses.
  • private: Private access rights, properties and methods can only be accessed within the class.

The following is an example of using access control:

class Car {
  public $brand;
  protected $price;
  private $mileage;
  
  public function __construct($brand, $price, $mileage) {
    $this->brand = $brand;
    $this->price = $price;
    $this->mileage = $mileage;
  }
  
  public function displayInformation() {
    echo "Brand: " . $this->brand . "<br>";
    echo "Price: " . $this->price . "<br>";
    echo "Mileage: " . $this->mileage . "<br>";
  }
  
  protected function calculateValue() {
    return $this->price - $this->mileage;
  }
  
  private function getMileage() {
    return $this->mileage;
  }
}

$car = new Car("Toyota", 20000, 50000);

$car->displayInformation(); // 输出:Brand: Toyota   Price: 20000   Mileage: 50000
echo $car->brand; // 输出:Toyota

// 以下行代码会报错,因为$price和$mileage属性具有受保护和私有访问权限,无法在类外部访问:
//echo $car->price;
//echo $car->mileage;

// 以下行代码会报错,因为calculateValue和getMileage方法具有受保护和私有访问权限,无法在类外部访问:
//echo $car->calculateValue();
//echo $car->getMileage();

In this example, we create a Car class with a public attribute brand, a protected attribute price and A private property mileage, a public method displayInformation, a protected method calculateValue and a private method getMileage.

Summary
In this article, we learned how to use object data types in PHP. We learned how to create a class, create an object, and access the object's properties and methods through arrow syntax. We also learned the concepts of class inheritance, encapsulation and access control and practiced them with sample code. By understanding and proficiently using object data types, we can better utilize PHP's object-oriented programming features to design and develop complex applications.

The above is the detailed content of How to use object data type in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn