The example in this article tells about stdclass usage in PHP. Share to everyone for your reference, specific as follows:
PHP Stdclass in our development applications, but the use of Stdclass in PHP is very large, let's look at the use of PHP stdclass.
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:
$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:
$tanteng = Array ();
$tanteng [' name '] = ' tanteng ';
$tanteng [' email '] = ' xxx@qq.com ';
You can understand that: Stdclass is a built-in class, it has no member variables, there is no member method of a class, the new one stdclass is to instantiate an "empty" object, it does not make sense in itself, but what is the advantage of using Stdclass definition?
The following code:
$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:
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, assigning to $myuser, copy a copy to the $myuser, which increases the overhead of the system.
You can, of course, convert an array to an object in turn:
$hehe [' he1 '] = ' he1 ';
$hehe [' he2 '] = ' he2 ';
$HH = (object) $hehe;
Print_r ($HH);
Print results:
StdClass Object ([he1] => he1 [he2] => he2)
More about PHP Interested readers can view the site topics: "PHP object-oriented Program Design Primer", "PHP basic Grammar Introductory Course", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Array" operation Skills Encyclopedia, " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips
I hope this article will help you with the PHP program design.