Home  >  Article  >  php教程  >  Example analysis of STDCLASS usage in PHP

Example analysis of STDCLASS usage in PHP

高洛峰
高洛峰Original
2016-12-21 14:31:411071browse

The example in this article describes 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 STDCLASS in PHP is very useful. Let’s take a look at the usage of STDCLASS in PHP.

In many places in WordPress, stdClass is used to define an object (usually in the form of an array), and then get_object_vars is used 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 an associative array composed of object attributes. 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 like this: stdClass is a built-in class. It has no member variables or member methods. A new stdClass instantiates an "empty" class. 』Object, it is meaningless in itself, 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 the value $user, but in fact, there is no new memory storage variable, $myUser Still referring to the stdClass object, changing the property page of $myUser changes the properties of $user, instead of creating a new copy. If there are many such operations in the program, 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 attributes of $myUser does change the stdClass attribute declared by $user. If $user is an array and is assigned to $myUser, then a copy is copied to $myUser. This 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 result:

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

I hope this article will be helpful to everyone in PHP programming.

For more articles related to the analysis of STDCLASS usage examples in PHP, please pay attention to 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