Usage of STDCLASS in PHP-PHP source code

Source: Internet
Author: User
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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.