Home > Backend Development > PHP Tutorial > In-depth understanding of PHP object-oriented programming: interaction between classes and objects

In-depth understanding of PHP object-oriented programming: interaction between classes and objects

WBOY
Release: 2024-06-01 11:20:56
Original
779 people have browsed it

Object-oriented programming (OOP) is a programming paradigm that encapsulates data and behavior in objects to represent real-world entities. In PHP, OOP allows the creation of classes and objects to represent entities in the real world: Classes: Define the data (properties) and operations (methods) of an object. Object: An instance of a class that contains the properties and methods of that class and can interact with other objects. OOP practical example: A shopping cart contains a series of products, modeled by the following two classes: Product: Represents a single product, with a name and price. Cart: Represents a shopping cart, containing a list of products and providing methods for adding products and calculating the total price.

In-depth understanding of PHP object-oriented programming: interaction between classes and objects

PHP In-depth understanding of object-oriented programming: interaction between classes and objects

What is object-oriented programming?

Object-oriented programming (OOP) is a programming paradigm that encapsulates data and behavior in objects. In PHP, OOP allows us to create classes and objects to represent entities in the real world.

Classes and Objects

  • Class: A class is a blueprint for a group of objects with common characteristics and behaviors. It defines the object's data (properties) and operations (methods).
  • Object: An object is an instance of a class. It contains the properties and methods of the class and can interact with other objects.

Class definition

class Person {
  private $name;
  private $age;

  public function __construct($name, $age) {
    $this->name = $name;
    $this->age = $age;
  }

  public function getName() {
    return $this->name;
  }

  public function setName($name) {
    $this->name = $name;
  }

  public function getAge() {
    return $this->age;
  }

  public function setAge($age) {
    $this->age = $age;
  }
}
Copy after login

Object creation

To create a human object, we can use The following code:

$person = new Person('John Doe', 30);
Copy after login

Accessing properties and methods

We can use the -> operator to access the properties and methods of an object:

echo $person->getName(); // 输出:"John Doe"

$person->setAge(35);
Copy after login

Practical Case: Shopping Cart

Consider an example of a shopping cart that contains a list of products:

class Product {
  private $name;
  private $price;

  public function __construct($name, $price) {
    $this->name = $name;
    $this->price = $price;
  }

  // ...
}

class Cart {
  private $products = [];

  public function addProduct(Product $product) {
    $this->products[] = $product;
  }

  public function getTotalPrice() {
    $totalPrice = 0;
    foreach ($this->products as $product) {
      $totalPrice += $product->getPrice();
    }

    return $totalPrice;
  }

  // ...
}
Copy after login

We can use these classes to create a shopping cart And add products:

$cart = new Cart();
$product1 = new Product('Apple', 10);
$product2 = new Product('Orange', 5);

$cart->addProduct($product1);
$cart->addProduct($product2);

echo $cart->getTotalPrice(); // 输出:"15"
Copy after login

This object-oriented approach allows us to create reusable and maintainable programs to represent and manipulate data and behavior in real-world scenarios.

The above is the detailed content of In-depth understanding of PHP object-oriented programming: interaction between classes and objects. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template