PHP's stdClass

Outline

The following is a description of the php Baidu Encyclopedia of the stdClass:

stdClass began to be popular in PHP5. The stdClass zend is a reserved class. stdClass PHP class is an internal class reservations, initially no member variables nor member methods, all of the magic methods are set to NULL. Those who use new stdClass () variables, are not likely to occur $ a-> test ( ) use this approach. PHP5 object uniqueness, called object anywhere, is a reference address type, so the relative consumption of resources will be less. Is directly modified when other pages assigned to it, instead of referencing a copy.

In the Baidu search, there are a lot of people write articles, I mentioned stdClass is the base class for all classes, but I tried it:

class Test{}
$t = new Test();
var_dump($t instanceof stdClass);

Output:bool(false)

Obviously, not stdClass base class Test, that is not all stdClass base class.

So stdClass in the end is what is it? In the end you are doing with it?

Doubts

Since this class has no member variables and no audit magic method, then stdClass in the end What the hell?

$a = new stdClass();
$a->username = 'a';
$a->email = 'qq';
var_dump($a);

Output results:

class stdClass#1 (2) {
public $username =>
string(1) "a"
public $email =>
string(2) "qq"
}

Obviously, this is a complete data ah.

So what's the difference between it and the array is? I saw some people at night are explained, the following codes

$a = new stdClass();
$b = $a;

Such a memory ab two variables public, $b=$a;is a reference to the assignment, and if it is an array, it will copy the entire array of objects.

However, you forgot to take the address of a symbol of what? $b=&$a, Is also assigned by reference ah? What's the difference? Obviously, the above argument does not convince me, what kind of role stdClass is it? Where is the meaning of its existence in?

Etc., passed by reference? Symbols do not need to use the address? This is not completely available in the global variable parameters and methods it?

Of course, the parameters passed in the array method, the parameter used &$amay be passed by reference to the effect, but if the function does not use the address symbol transmission time will copy, memory is wasted. However, it must also bear the corresponding risk way to copy an array of course, a waste of memory, but regardless of how to operate within the array method, the array will not react to the outside in. If a stdClassclass is not the same, you pass go, we must do a good job out of time to prepare the property beyond recognition.

Global variables , if you define a global variable in a static method S, this variable is available to all, then use the array on the very authentic. You Get in a method of the object and set the attribute of the object, for use in the back. After the acquisition method b of this object again, if you are using an array, so embarrassing, S array method call is returned, only to revise its array of local variables were modified, and did not achieve the desired effect. If the S returns stdClass objects, then no problem, ah.

For chestnut:

class Test{
    private static $user = [];
    public static function getUser(){
        return self::$user;
    }
}
$a = Test::getUser();
$a['username'] = 'name';
$b = Test::getUser();
var_dump($b);

Output results:

array(0) {
}

In this case, use stdClass, the effect is entirely different:

class Test{
    private static $user = null;
    public static function getUser(){
        if(self::$user === null){
            self::$user = new stdClass();
        }
        return self::$user;
    }
}
$a = Test::getUser();
$a->username = 'name';
$b = Test::getUser();
var_dump(get_object_vars($b));

Output:

array(1) {
'username' =>
string(4) "name"
}

Perfect ~

stdClassFor this property to provide a global, but right then, in fact, can also use a singleton class to achieve the same method, but obviously use stdClassmore flexible and more concise.

You may be used get_object_vars($a)a method object into a talk stdClass array which speaks properties of the object transformed into an array.


At this point, I also think stdClassof scenarios.

I believe it has more application scenarios, after all, is a direct reference to the assignment, the object will be modified directly. But I think there is no better scenario, alas, ignorant.

Ladies and gentlemen, goodbye! !

Guess you like

Origin www.cnblogs.com/hujingnb/p/11735542.html