Home  >  Article  >  Java  >  Simple understanding of the stdClass class in php

Simple understanding of the stdClass class in php

怪我咯
怪我咯Original
2017-07-14 15:10:442017browse

stdClass only became popular in PHP5. And stdClass is also a reserved class of zend. stdClass is a base class of PHP. Almost all classes inherit this class, so it can be new at any time and this variable can become an object. At the same time, this base class has a special feature, that is, it has no methods. Any variable that uses new stdClass() will not be used in this way $a->test(). The uniqueness of PHP5's object is that when the object is called anywhere, it is of reference address type, so it consumes less resources. When assigning values ​​to it on other pages, it is modified directly instead of referencing a copy. This article mainly introduces the stdClass class in PHP. It introduces the stdClass class in vernacular. Friends who need it can refer to the code like

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

. What is it used for?
Open the manual and searchstdClass. You will find that there is almost no introduction in the manual. If you search Google again, you will see almost all English explanations.
In fact, stdClass only became popular in PHP5. And stdClass is also a reserved class of zend. It seems to have no other effect. There is also almost no explanation.
Or, we can understand it this way: stdClass is a base class of PHP. Almost all classes inherit this class, so it can be new at any time, and this variable can become a object. At the same time, this base class has a special feature, that is, it has no methods.
Whenever a variable of new stdClass() is used, it is impossible to use $a->test().
Or, we can understand it this way. Because of the uniqueness of PHP5's object, when the object is called anywhere, it is a reference address type, so it will consume less resources. When assigning values ​​to it in other pages, it is modified directly instead of referencing a copy.
For example:

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

If in the PHP4 era, such code is consuming system resources. Because:
$myUser = $user;
This creates a copy. Therefore, in PHP4, it is always used like this:

$myUser = & $user;

Some people say, why not use array? Wouldn't arrays be more convenient? And for weakly typed programs like PHP, using arrays should be the most convenient.

The above is the detailed content of Simple understanding of the stdClass class in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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