StdClass usage analysis in php _ PHP Tutorial

Source: Internet
Author: User
Analysis of stdClass usage in php. StdClass usage analysis in php this article mainly introduces the usage of stdClass in php. The example analyzes the functions and usage skills of stdClass, which has some reference value, if you need a friend, you can refer to the usage analysis of stdClass in php.

This article mainly introduces the usage of stdClass in php. The example analyzes the functions and usage skills of stdClass, which has some reference value. For more information, see

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:

?

1

2

3

4

5

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

?

1

2

3

4

5

6

$ A = new stdClass ();

$ A-> id = '11 ';

$ A-> username = 'me ';

Print_r ($ );

?>

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:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

StdClass Object

(

[GetWeatherbyCityNameResult] => stdClass Object

(

[String] => Array

(

[0] => Sichuan

[1] => Chengdu

[2] => 56294.

[3] => 56294.jpg

[4] => 13:52:08

[5] => 26 ℃/19 ℃

[6] => cloudy to cloudy with showers in May 17

)

)

)

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:

?

1

2

3

4

5

6

7

$ A = array (1 => 2, 2 => 3 );

$ A = (object) $;

$ A-> id = '11 ';

$ A-> username = 'me ';

Print_r ($ );

?>

Output: stdClass Object ([1] => 2 [2] => 3 [id] => 11 [username] => me ).

?

1

2

3

4

5

6

7

8

$ A = array (1 => 2, 2 => 3 );

$ A = (object) $;

$ A = new stdClass ();

$ A-> id = '11 ';

$ A-> username = 'me ';

Print_r ($ );

?>

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.

This article introduces the usage of stdClass in php. The example analyzes the functions and usage skills of stdClass, which has some reference value. if you need it, you can refer to it...

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.