Home > Backend Development > PHP Problem > How to create an array of class objects in php

How to create an array of class objects in php

PHPz
Release: 2023-04-12 09:59:58
Original
815 people have browsed it

In PHP, we can use class object array object array object to store and manage large amounts of data, which can make the code more modular and reusable. The process of creating an object-like array object is relatively simple. Let’s learn step by step.

  1. Define class

Before creating an array object of class objects, we need to define a class first. A class is a code template that can be used to create objects. Here is a sample class definition:

class Person {
  public $name;
  public $age;
  
  function __construct($name, $age) {
    $this->name = $name;
    $this->age = $age;
  }
  
  function hello() {
    echo "Hello, my name is " . $this->name . " and I am " . $this->age . " years old.";
  }
}
Copy after login

This class defines a class named Person, which has two attributes name and age, and two methods __construct() and hello(). Among them, __construct() is a special method that is automatically called when the object is created.

  1. Create an array of class objects

Now that we have defined a class, we can use this class to create an array of class objects. An array of class objects is an array that stores class objects, and each element is an instance of a class. The following is an example of creating an array of class objects:

$people = array(
  new Person("Tom", 30),
  new Person("Jerry", 25),
  new Person("Mickey", 20)
);
Copy after login

In the above example, we use the new keyword to create three Person class objects and add They are stored in the $people array. These class objects can now be accessed by accessing elements in the array.

  1. Create a class object array object

In some cases, we need to combine multiple class object arrays into a class object array object. A class object array object is an array that stores an array of class objects. Each element is an array of class objects. Here is an example of creating a class object array object:

$family = array(
  array(
    new Person("Tom", 30),
    new Person("Mary", 25)
  ),
  array(
    new Person("Jerry", 40),
    new Person("Lucy", 35),
    new Person("Jack", 10)
  )
);
Copy after login

In the above example, we have created a class object array object named $family, which contains two elements, Each element is an array of Person class objects.

  1. Accessing the class object array object

Now that we have created a class object array object, we can access them by accessing the properties and methods of the array and class object. Here is an example of accessing the $people array and the $family array:

echo $people[0]->name; // 输出 "Tom"
echo $family[0][0]->name; // 输出 "Tom"
echo $family[0][1]->name; // 输出 "Mary"
echo $family[1][0]->name; // 输出 "Jerry"
echo $family[1][1]->name; // 输出 "Lucy"
echo $family[1][2]->name; // 输出 "Jack"
Copy after login

In the above example, we accessed the $people array and the class objects in the $family array, and output the values ​​of their properties name.

Summary

This article describes how to create array objects of class objects and access their properties and methods. To create a class object, you need to first define a class, and then use the new keyword to create an array of class objects or a class object. When accessing class objects, you can access arrays and class objects by accessing their properties and methods. Creating and accessing class objects is a very common operation in PHP programming, which can help us better manage and process large amounts of data.

The above is the detailed content of How to create an array of class objects in php. For more information, please follow other related articles on the PHP Chinese website!

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