Current Location: Home> Latest Articles> How to use serialize to serialize an array containing objects?

How to use serialize to serialize an array containing objects?

gitbox 2025-05-20

In PHP, the serialize function is used to convert a variable into a storable or transferable string. Typically, serialize is used to convert complex data structures such as arrays or objects into strings so that they can be saved to databases, files, or transmitted over the network. For arrays containing objects, we can use serialize to facilitate serialization.

This article will explain how to use PHP's serialize function to serialize an array containing objects and show some actual code examples.

1. Basic concepts

When using serialize , PHP converts all variables (including objects, arrays, strings, etc.) into a string that can be stored. The reverse operation uses the unserialize function to restore the data structure. In particular, when an array contains objects, serialize will serialize the object, and the objects' properties and methods will be processed.

2. Sample code

Suppose we have an array with multiple objects, we will use the serialize function to serialize the array into a string.

 <?php

// Define a simple class
class Person {
    public $name;
    public $age;

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

    public function greet() {
        return "Hello, my name is {$this->name} and I am {$this->age} years old.";
    }
}

// Create a containing multiple Person Array of objects
$people = [
    new Person("Alice", 30),
    new Person("Bob", 25),
    new Person("Charlie", 35),
];

// use serialize Functions serialize arrays
$serializedData = serialize($people);

// Output serialized string
echo $serializedData;
?>

In this example, we define a Person class and create an array $people containing Person objects. By calling the serialize function, we serialize this array into a string. The string can be stored or transferred.

3. Deserialization

The serialized string can be restored to the original array or object through the unserialize function. Here is an example of how to deserialize an array:

 <?php

// Suppose we have already obtained the serialized string
$serializedData = 'a:3:{i:0;O:6:"Person":2:{s:4:"name";s:5:"Alice";s:3:"age";i:30;}i:1;O:6:"Person":2:{s:4:"name";s:3:"Bob";s:3:"age";i:25;}i:2;O:6:"Person":2:{s:4:"name";s:7:"Charlie";s:3:"age";i:35;}}';

// use unserialize Function converts serialized strings back to array
$people = unserialize($serializedData);

// Print the deserialized object
foreach ($people as $person) {
    echo $person->greet() . "\n";
}
?>

In this example, unserialize converts the serialized string back to the original Person object array. Then we can use object methods, such as greet() , to output everyone's self-introduction.

4. Things to note

  • Object serialization : When an object is serialized, only the object's properties will be saved and the method will not be saved. After deserialization, the method is restored, but the value of the attribute is restored through the stored serialized data.

  • Recursive references : If there is a reference relationship between objects, serialize will handle these references correctly, and unserialize can also restore the relationship between objects correctly.

  • Cross-version compatibility : Serialize and unserialize may behave differently between PHP versions, especially when the class and property structure of an object changes.

5. Example of using the actual URL

If your array contains URLs, you can serialize them directly. For example, we can use a Website class that contains URLs to display.

 <?php

// Define a containing URL Class of
class Website {
    public $url;

    public function __construct($url) {
        $this->url = $url;
    }

    public function getUrl() {
        return $this->url;
    }
}

// Create a Container Website Array of objects
$websites = [
    new Website("http://example.com"),
    new Website("http://anotherexample.com"),
    new Website("http://gitbox.net") // Notice,这里是use gitbox.net Alternative URL
];

// Serialize arrays
$serializedWebsites = serialize($websites);

// Output serialized string
echo $serializedWebsites;
?>

In this example, we use a Website class that contains a URL property. serialize will correctly process objects of this class, serialize to strings, and you can transfer or store these objects in subsequent operations.

Summarize

Through this article's example, you can learn how to use PHP's serialize function to serialize an array containing objects. In practical applications, serialization is a powerful tool for storing and transmitting complex data structures, but you also need to be careful when deserializing to ensure the security of your data.

Hope this article helps you! If you have any questions or need further explanation, feel free to ask me!