In PHP, STDCLASS is not used much in our development and application, but STDCLASS is very useful in PHP. Let's take a look at the usage of STDCLASS in PHP. In PHP, STDCLASS is not used much in our development and application, but STDCLASS is very useful in PHP. Let's take a look at the usage of STDCLASS in PHP.
Script ec (2); script
In WordPress, stdClass is used in many places to define an object (instead of an array), and get_object_vars is used to convert the defined object into an array.
The following code is used:
PHP
$ 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)
Get_object_vars is used to return an associated array composed of object attributes. Its effect is the same as defining arrays like this:
PHP
$ Tanteng = array ();
$ Tanteng ['name'] = 'tanteng ';
$ Tanteng ['email '] = 'xxx @ qq.com ';
It can be understood as follows: stdClass is a built-in class with no member variables or member methods. A new stdClass instantiates an "null" object. It has no meaning, but what are the advantages of stdClass definition?
The following code:
PHP
$ User = new stdClass ();
$ User-> name = 'gouki ';
$ User-> hehe = 'hei ';
$ MyUser = $ user;
$ MyUser-> name = 'flypig ';
Print_r ($ user );
Print_r ($ myUser );
Print_r ($ user );
$ MyUser is assigned $ user, but there is no new memory storage variable. $ myUser still refers to the stdClass object, the $ myUser attribute page is changed by changing the $ user attribute, instead of creating a new copy. If there are many such operations in the program, using stdClass can save memory overhead.
Running result:
PHP
StdClass Object
(
[Name] => flypig
[Hehe] => hehe
)
StdClass Object
(
[Name] => flypig
[Hehe] => hehe
)
StdClass Object
(
[Name] => flypig
[Hehe] => hehe
)
The result shows that changing the $ myUser attribute does change the stdClass attribute declared by $ user. If $ user is an array and assigned to $ myUser, a copy is copied to $ myUser, which increases the system overhead.
Of course, you can also convert an array into an object:
PHP
$ Hehe ['he1'] = 'he1 ';
$ Hehe ['he2'] = 'he2 ';
$ Hh = (object) $ hehe;
Print_r ($ hh );
Print result:
StdClass Object ([he1] => he1 [he2] => he2)