Usage analysis of Stdclass in PHP, Phpstdclass usage
This paper analyzes the usage of stdclass in PHP. Share to everyone for your reference. The specific analysis is as follows:
Stdclass is one of several predefined classes in PHP and is a class reserved by Zent. In fact, it is a base class provided by PHP, a blank class, nothing in it, we can instantiate it, and then define a series of variables to pass through it (many PHP programmers use it to pass the values of a series of variables, while at the same time lazy to create their own class). However, because methods cannot be added after instantiation, only properties can be passed. Because once the class is actually listed, you cannot add a method.
Stdclass can be used as a base class, and its greatest feature is that (its derived classes) can automatically add member variables without having to be described at the time of definition.
All PHP variables are examples of stdclass.
How to use:
1. Using Stdclass:
$andy = Array (), $andy = (object) $andy; $andy->a = 1; $andy->b = 2; $andy->c = 3;
So the quantity A, B, C is filled into the stdclass inside. This is easy, because the new empty pair is $andy = new Andy; And you have to have a class andy{} first. Another example:
<?php$a = new StdClass (); $a->id = ' one '; $a->username = ' Me ';p rint_r ($a);? >
Will output: StdClass Object ([id] = = [Username] = me).
Many times this method replaces the use of arrays, but is a different form of syntax.
2. READ:
StdClass object ( [Getweatherbycitynameresult] = StdClass object ([ string] = = Array ( [0] = Sichuan [1] = Chengdu [2] = 56294 [3] = 56294.jpg [4] = 2009-5-17 13:52:08 [5] = 26 ℃/1 9 ℃ [6] = May 17 Cloudy turn showers)) )
In fact, the same as the array, just the access to change a little bit, we are generally accustomed to use array[' key ' this way to access the array.
For this stdclass, as in the example above, $weather->getweatherbycitynameresult->string[0] can access the property in this way, this will result in "Sichuan".
3, instantiation, new.
Compare these two codes:
<?php $a = Array (1=>2,2=>3), $a = (object) $a; $a->id = ' one '; $a->username = ' Me ';p rint_r ($a);? >
Output: StdClass Object ([1] = 2 [2] = 3 [id] = = [Username] = me).
<?php $a = Array (1=>2,2=>3), $a = (object) $a; $a = new StdClass (); $a->id = ' one '; $a->username = ' me ';p ri Nt_r ($a);? >
Output: StdClass Object ([id] = = [Username] = me).
Originally instantiated with new, the previous array is emptied, leaving only the following additions, and if not instantiated, Stdclass retains all elements.
It should be noted that when using global, static in the function of the case of new Stdclass reference, then &new Stdclass will be invalidated, should avoid using the reference, directly with the new Stdclass.
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/961389.html www.bkjia.com true http://www.bkjia.com/PHPjc/961389.html techarticle The usage analysis of Stdclass in PHP, Phpstdclass usage This article analyzes the usage of stdclass in PHP. Share to everyone for your reference. The specific analysis is as follows: Stdclass is predefined in PHP ...