How to use Stdclass in PHP

Source: Internet
Author: User

Many places in WordPress use Stdclass to define an object (usually in the form of an array), and then use Get_object_vars to "convert" the defined object to a number of groups.


As shown in the following code:

Php

$tanteng = new StdClass ();
$tanteng->name = ' Tanteng ';
$tanteng->email = ' xxx@qq.com ';

$info = Get_object_vars ($tanteng);
Print_r ($info); exit;
Output:

Array ([name] => Tanteng [email] => xxx@qq.com)

The role of Get_object_vars is to return an associative array of object properties. Its effect is the same as defining an array in this way:

Php

$tanteng = Array ();
$tanteng [' name '] = ' tanteng ';
$tanteng [' email '] = ' xxx@qq.com ';
You can understand this: Stdclass is a built-in class that has no member variables and no member methods of a class. New A stdclass is to instantiate an "empty" object, it doesn't make sense in itself, but what's the benefit of using Stdclass definition?

The following code:

Php

$user = new StdClass ();
$user->name = ' Gouki ';
$user->hehe = ' hehe ';
$myUser = $user;
$myUser->name = ' Flypig ';

Print_r ($user);

Print_r ($myUser);

Print_r ($user);
Here $myuser is assigned to $user, but in fact, does not create a new memory storage variables, $myUser or refers to the Stdclass this object, through the $myuser change the property page changed the properties of the $user, not a new copy, if the program has many such operations , you can save memory overhead by using the Stdclass method.

Run Result:

Php

StdClass Object
(
[Name] => Flypig
[hehe] => hehe
)
StdClass Object
(
[Name] => Flypig
[hehe] => hehe
)
StdClass Object
(
[Name] => Flypig
[hehe] => hehe
)
As you can see from the results, changing the properties of $myuser does change the Stdclass attribute of the $user declaration. And if $user is an array, assigned to $myuser, then a copy is copied to the $myuser, which increases the overhead of the system.

You can, of course, convert an array to an object in turn:

Php

$hehe [' he1 '] = ' he1 ';
$hehe [' he2 '] = ' he2 ';

$HH = (object) $hehe;

Print_r ($HH);
Print results:

StdClass Object ([he1] => he1 [he2] => he2)

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.