Home >Backend Development >PHP Problem >How to convert PHP class to array object

How to convert PHP class to array object

PHPz
PHPzOriginal
2023-04-19 09:22:28903browse

PHP is a high-level programming language that provides a large number of built-in functions and features to facilitate developers to write efficient and maintainable code. There are many objects in the PHP language, and these objects can contain multiple properties and methods. For a developer, converting an object into an array is very useful, it allows us to have more flexibility in processing.

This article will introduce how to convert a PHP class (or object) into an array object. In this article, we will explore some best practices and conventions for PHP code.

  1. Convert an object to an array

The first way to convert an object to an array is to use the get_object_vars() function. This function will return an associative array of object properties.

class User {
    public $name;
    protected $email;
    private $password;
    
    public function __construct($name, $email, $password) {
        $this->name = $name;
        $this->email = $email;
        $this->password = $password;
    }
}

$user = new User('Tom', 'tom@example.com', '123456');
$userArray = get_object_vars($user);

print_r($userArray);    // 输出: Array ( [name] -> Tom )

In the above code, the get_object_vars() function only returns the $name attribute because $email and $password Properties are protected or private.

  1. Convert a class into an array

The way to convert a PHP class into an array object is to use the get_class_methods() function to get the method list of the object. It then loops through all the public properties and methods of the object and outputs them as an array.

class User {
    public $name;
    protected $email;
    private $password;
    
    public function __construct($name, $email, $password) {
        $this->name = $name;
        $this->email = $email;
        $this->password = $password;
    }

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

    protected function getEmail() {
        return $this->email;
    }

    private function getPassword() {
        return $this->password;
    }
}

$user = new User('Tom', 'tom@example.com', '123456');

$objectVars = get_object_vars($user);   
$classMethods = get_class_methods($user);  
$userInfo = array();   

foreach ($classMethods as $methodName) {
    if (substr($methodName, 0, 3) == 'get') {
        $propertyName = lcfirst(substr($methodName, 3));
        if (array_key_exists($propertyName, $objectVars)) {
            $userInfo[$propertyName] = $user->$methodName();
        }
    } 
}

print_r($userInfo);

In the above code, the $classMethods variable contains all public methods of the object, all methods are traversed through a loop, and the method names are converted into attribute names. If the property exists in the $objectVars variable, the property is added to the $userInfo array.

  1. Serialization and deserialization of object arrays

It is very convenient to serialize object arrays into strings. There is a predefined function serialize() in PHP that can serialize a PHP object into a string, which can be saved to a file or database. To restore a serialized object to a PHP object, you can use the unserialize() function.

$user = new User('Tom', 'tom@example.com', '123456');
$userArray = array('name' => $user->name, 'email' => $user->email, 'password' => $user->password);

$serializeUser = serialize($userArray);

// 反序列化为对象数组
$unserializeUser = unserialize($serializeUser);

print_r($unserializeUser);

In the above code, $userArray is an array object that contains all the properties of the $user object. PHP serialize() function serializes array objects into string storage. During deserialization, you can use the unserialize() function to restore the original array object to a PHP object.

Summary

This article introduces how to convert PHP classes and objects into array objects, and how to serialize and deserialize PHP object arrays. These technologies allow us to handle PHP objects more flexibly and conveniently in many situations. These techniques are very useful when we need to process PHP objects and store them in a database or on the hard disk. We hope these techniques are helpful in your work.

The above is the detailed content of How to convert PHP class to array object. 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