What is stdClass in PHP?
What is stdClass in PHP?
In PHP, stdClass
is a built-in class that represents an object without a defined class. It is commonly used when an object is created without specifying a class or when an object is converted from an array using the (object)
cast.
Here are some key points about stdClass
in PHP:
When an object is created without specifying a class, PHP automatically creates an instance of stdClass
for that object. For example:
$obj = new stdClass();
$obj->name = "John";
$obj->age = 30;
In this case, $obj
is an instance of stdClass
with two properties: name
and age
.
stdClass
is also used when an array is converted to an object using the (object)
cast. For example:
$array = ['name' => 'John', 'age' => 30];
$obj = (object) $array;
After the conversion, $obj
is an instance of stdClass
with properties corresponding to the keys of the original array.
Objects of stdClass
can have properties added dynamically at runtime. This is because stdClass
do...
junior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào