This article mainly introduces the stdClass class in PHP, and introduces the stdClass class in the vernacular. if you need it, you can refer to it. I believe you will often see PHP code similar to the following:
$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 instead of referencing a copy.
For example:
$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:
$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.
For more information about stdClass articles in PHP, refer to the PHP Chinese website!