Understanding stdClass in PHP

Source: Internet
Author: User
This article mainly introduces the stdClass class in PHP, and introduces the stdClass class in vernacular. If you need it, refer

This article mainly introduces the stdClass class in PHP, and introduces the stdClass class in vernacular. If you need it, refer

I believe that, like me, you will often see PHP code similar to the following:

The Code is as follows:


$ User = new stdClass ();
$ User-> name = 'gouki ';

Why is this code used?
Open the manual and search for stdClass. You will find that there is almost no introduction in the manual. If you search for google again, you will see almost all the explanations in English.
In fact, stdClass became popular in PHP5. StdClass is also a reserved class of zend. It does not seem to have any other effect. There is almost no explanation.
Alternatively, we can understand that stdClass is a base class of PHP. Almost all classes inherit this class, so it can be new at any time and make this variable an object. At the same time, this base class has another special point, that is, there is no method.
When new stdClass () is used, $ a-> test () cannot be used.
Or, let's take another look at this. Because of the uniqueness of PHP5 objects, objects can be called anywhere and are referenced in the address type, so they consume less resources. When assigning values to other pages, it is directly modified, rather than referencing a copy.
For example:

The Code is as follows:


$ User = new stdClass ();
$ User-> name = 'gouki ';
$ MyUser = $ user;
$ MyUser-> name = 'flypig ';


In the PHP4 era, such code is consuming system resources. Because:
$ MyUser = $ user;
This is a copy. Therefore, it is used in PHP4 as follows:

The Code is as follows:


$ MyUser = & $ user;


Some people say, why not use arrays? Isn't array more convenient? In addition, for a weak type program such as PHP, array should be the most convenient.
Yes. The array should be the most convenient in the use of the program. However, every time the array is referenced ($ a = $ B), a copy is actually created, and after the array is unset, it still occupies the memory (I didn't test it ...... I don't know how to test it either. If anyone knows, please let me know. Thank you)
However, there is also a function arrayobject in the standard class library of SPL, which can directly convert arrays into objects. This is also a good method.

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.