What is stdClass in PHP?

2018-12-31 03:46发布

Please define what stdClass is.

标签: php stdclass
16条回答
像晚风撩人
2楼-- · 2018-12-31 04:05

is a way in which the avoid stopping interpreting the script when there is some data must be put in a class , but unfortunately this class was not defined

Example :

 return $statement->fetchAll(PDO::FETCH_CLASS  , 'Tasks');

Here the data will be put in the predefined 'Tasks' . But, if we did the code as this :

 return $statement->fetchAll(PDO::FETCH_CLASS );

then the will put the results in .

simply says that : look , we have a good KIDS[Objects] Here but without Parents . So , we will send them to a infant child Care Home :)

查看更多
临风纵饮
3楼-- · 2018-12-31 04:06

Actually I tried creating empty stdClass and compared the speed to empty class.

class emp{}

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).

查看更多
不再属于我。
4楼-- · 2018-12-31 04:06

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 using instanceof operator or function get_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.

查看更多
忆尘夕之涩
5楼-- · 2018-12-31 04:07

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:

class Foo{}
$foo = new Foo();
echo ($foo instanceof stdClass)?'Y':'N';
// outputs 'N'

I don't believe there's a concept of a base object in PHP

查看更多
君临天下
6楼-- · 2018-12-31 04:09

If you wanted to quickly create a new object to hold some data about a book. You would do something like this:

$book = new stdClass;
$book->title = "Harry Potter and the Prisoner of Azkaban";
$book->author = "J. K. Rowling";
$book->publisher = "Arthur A. Levine Books";
$book->amazon_link = "http://rads.stackoverflow.com/amzn/click/0439136369";

Please check the site - http://www.webmaster-source.com/2009/08/20/php-stdclass-storing-data-object-instead-array/ for more details.

查看更多
梦寄多情
7楼-- · 2018-12-31 04:09

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

//example 1
$data=array('k1'=>'v1' , 'k2'=>'v2',....);

//example 2
//creating an empty class is faster than instances an stdClass
class data={}
$data=new data();
$data->k1='v1';
$data->k2='v2';

what makes someone to think about using the object style instead of array style???

查看更多
登录 后发表回答