Analysis of stdClass usage in php and phpstdclass usage. Analysis of stdClass usage in php, phpstdclass usage in this article examples analysis of stdClass usage in php. Share it with you for your reference. The specific analysis is as follows: stdclass is a predefined stdClass usage analysis in php, and phpstdclass usage
This article analyzes the usage of stdClass in php. Share it with you for your reference. The specific analysis is as follows:
Stdclass is one of the predefined classes in php and is a class reserved by zent. In fact, it is a base class provided by PHP. it is a blank class with nothing in it. we can instantiate it and define a series of variables, it is used to pass variables (many php programmers use it to pass a series of variable values, while they are too lazy to create their own classes ). However, attributes can only be passed because the method cannot be added after instantiation. Because once the class is listed in real form, the method cannot be added.
Stdclass can be used as a base class. its biggest feature is that (its derived class) can automatically add member variables without being explained during definition.
All php variables are stdClass instances.
Usage:
1. use stdclass:
$andy = array();$andy = (object)$andy;$andy->a = 1;$andy->b = 2;$andy->c = 3;
In this way, numbers a, B, and c are entered in stdclass. This saves time because $ andy = new Andy is required for creating an empty object, and a class Andy {} is required first {}. Another example:
<?php$a = new stdClass();$a->id = '11 ';$a->username = 'me';print_r($a);?>
The output is: stdClass Object ([id] => 11 [username] => me ).
In many cases, this method is used to replace the use of arrays, except for a syntax.
2. read:
StdClass Object ([getWeatherbyCityNameResult] => stdClass Object ([string] => Array ([0] => Sichuan [1] => Chengdu [2] => 56294 [3] => 56294.jpg [4] => May 17 13:52:08 [5] => 26 deg C/19 deg C [6] => overcast to shower )))
In fact, it is similar to array, but the access method changes a little. we generally use array ['key'] to access the array.
For this type of stdClass, for example, $ weather-> getWeatherbyCityNameResult-> string [0] can access the attribute in this way. The result is "Sichuan ".
3. instantiation, new.
Compare the two codes:
<?php $a = array(1=>2,2=>3);$a = (object)$a;$a->id = '11 ';$a->username = 'me';print_r($a);?>
Output: stdClass Object ([1] => 2 [2] => 3 [id] => 11 [username] => me ).
<?php $a = array(1=>2,2=>3);$a = (object)$a;$a = new stdClass();$a->id = '11 ';$a->username = 'me';print_r($a);?>
Output: stdClass Object ([id] => 11 [username] => me ).
After new is instantiated, the previous array is cleared, leaving only the elements added later. if not instantiated, stdClass retains all elements.
It should be noted that when global and static functions are used in the function, the new stdclass will be referenced. in this case, & new stdclass will become invalid, so reference should be avoided and new stdclass should be used directly.
I hope this article will help you with php programming.
In this article, we analyze the usage of stdClass in php. Share it with you for your reference. The specific analysis is as follows: stdclass is predefined in php...