Home  >  Article  >  Backend Development  >  What is STDCLASS in php? how to use?

What is STDCLASS in php? how to use?

伊谢尔伦
伊谢尔伦Original
2017-06-30 10:10:203694browse

This article mainly introduces the usage of STDCLASS in PHP, and analyzes the functions, principles, usage methods and related aspects of STDCLASS in PHP in the form of examples. Notes, friends in need can refer to the following

The examples in this article describe the usage of STDCLASS in PHP. Share it with everyone for your reference. The details are as follows:

STDCLASS in PHP is not used much in our development applications, but the role of STDCLASS in PHP is very large. Let’s take a look at STDCLASS in PHP. Usage.

stdClass is used in many places in WordPress to define a object (usually in the array way), and then use get_object_vars to "convert" the defined object into an array.

The following code is shown:

$tanteng = new stdClass();
$tanteng->name = 'tanteng';
$tanteng->email = 'xxx@qq.com';
$info = get_object_vars($tanteng);
print_r($info);
exit;

Output:

Array ( [name] => tanteng [email] => xxx@qq.com )

The function of get_object_vars is to return the object properties. Associative array. Its effect is actually the same as defining an array like this:

$tanteng = array();
$tanteng['name'] = 'tanteng';
$tanteng['email'] = 'xxx@qq.com';

It can be understood this way: stdClass is a built-in class, it has no members variables , and no member methods A new stdClass is a class that instantiates an "empty" object. It itself has no meaning, but what are the benefits of using stdClass to define it?

The following code:

$user = new stdClass();
$user->name = 'gouki';
$user->hehe = 'hehe';
$myUser = $user;
$myUser->name = 'flypig';
print_r($user);
print_r($myUser);
print_r($user);

Here$myUser is assigned $user, but in fact, a new memory storage variable is not opened. $myUser still refers to the stdClass object. Changing the property page through $myUser changes the properties of $user. It does not create a new copy. If there are many in the program For such an operation, using stdClass can save memory overhead.

Running results:

stdClass Object
(
  [name] => flypig
  [hehe] => hehe
)
stdClass Object
(
  [name] => flypig
  [hehe] => hehe
)
stdClass Object
(
  [name] => flypig
  [hehe] => hehe
)

It can be seen from the results that changing the properties of $myUser does change the stdClass property declared by $user. And if $user is an array and is assigned to $myUser, then a copy is copied to $myUser, which increases system overhead.

Of course, you can also do the opposite and convert an array into an object:

$hehe['he1'] = 'he1';
$hehe['he2'] = 'he2';
$hh = (object) $hehe;
print_r($hh);

Print results:

stdClass Object ( [he1] => he1 [he2] => he2 )

The above is the detailed content of What is STDCLASS in php? how to use?. 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