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 a way in which the php avoid stopping interpreting the script when there is some data must be put in a class , but unfortunately this class was not defined
Example :
Here the data will be put in the predefined 'Tasks' . But, if we did the code as this :
then the php will put the results in stdclass .
simply php says that : look , we have a good KIDS[Objects] Here but without Parents . So , we will send them to a infant child Care Home stdclass :)
Actually I tried creating empty stdClass and compared the speed to empty class.
then proceeded creating 1000 stdClasses and emps... empty classes were done in around 1100 microseconds while stdClasses took over 1700 microseconds. So I guess its better to create your own dummy class for storing data if you want to use objects for that so badly (arrays are a lot faster for both writing and reading).
stdClass is not an anonymous class or anonymous object
Answers here includes expressions that
stdClass
is an anonymous class or even anonymous object. It's not a true.stdClass
is just a regular predefined class. You can check this usinginstanceof
operator or functionget_class
. Nothing special goes here. PHP uses this class when casting other values to object.In many cases where
stdClass
is used by the programmers the array is better option, because of useful functions and the fact that this usecase represents the data structure not a real object.stdClass
is just a generic 'empty' class that's used when casting other types to objects. Despite what the other two answers say,stdClass
is not the base class for objects in PHP. This can be demonstrated fairly easily:I don't believe there's a concept of a base object in PHP
If you wanted to quickly create a new object to hold some data about a book. You would do something like this:
Please check the site - http://www.webmaster-source.com/2009/08/20/php-stdclass-storing-data-object-instead-array/ for more details.
stClass is an empty class created by php itself , and should be used by php only, because it is not just an "empty" class , php uses stdClass to convert arrays to object style if you need to use stdClass , I recommend two better options : 1- use arrays (much faster than classes) 2- make your own empty class and use it
what makes someone to think about using the object style instead of array style???