Please define what stdClass
is.
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
stdClass
is PHP's generic empty class, kind of likeObject
in Java orobject
in Python (Edit: but not actually used as universal base class; thanks @Ciaran for pointing this out).It is useful for anonymous objects, dynamic properties, etc.
An easy way to consider the StdClass is as an alternative to associative array. See this example below that shows how
json_decode()
allows to get an StdClass instance or an associative array. Also but not shown in this example,SoapClient::__soapCall
returns an StdClass instance.See Dynamic Properties in PHP and StdClass for more examples.
Also worth noting, an
stdClass
object can be created from the use ofjson_decode()
as well.Please bare in mind that 2 empty stdClasses are not strictly equal. This is very important when writing mockery expectations.
php.net manual has a few solid explanation and examples contributed by users of what stdClass is, I especially like this one http://php.net/manual/en/language.oop5.basic.php#92123, https://stackoverflow.com/a/1434375/2352773.
Likewise,
yields a stdClass object - auto casted
I found this out today by misspelling:
Cool!
Its also worth noting that by using Casting you do not actually need to create an object as in the answer given by @Bandula. Instead you can simply cast your array to an object and the stdClass is returned. For example:
Output: again