Home > php教程 > PHP开发 > body text

Example analysis of STDCLASS usage in PHP

高洛峰
Release: 2016-12-21 14:31:41
Original
1105 people have browsed it

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;
Copy after login

Output:

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

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';
Copy after login

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);
Copy after login

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
)
Copy after login

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);
Copy after login

Print result:

stdClass Object ( [he1] => he1 [he2] => he2 )
Copy after login

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!


Related labels:
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!