A detailed explanation of stdclass usage in PHP

Source: Internet
Author: User
This function is automatically loaded when the PHP kernel initializes the module, so that the registration of the Stdclass class is executed. The Stdclass class is a class that has no member variables and no member methods. All of its magic methods, parent classes, interfaces, etc. are set to null at initialization time. Since we cannot dynamically add methods to a class in PHP, this class can only be used to handle dynamic properties, which is a common usage. Therefore, the method does not trigger the set interceptor.

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 rin T_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.

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.