Câu hỏi phỏng vấn PHP
Câu hỏi

What is stdClass in PHP?

Câu trả lời

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:

1. Default class for objects

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.

2. Conversion from arrays

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.

3. Dynamic properties

Objects of stdClass can have properties added dynamically at runtime. This is because stdClass do...

junior

junior

Gợi ý câu hỏi phỏng vấn

junior

What does the 'var' keyword mean in PHP?

junior

How can you enable error reporting in PHP?

senior

What does yield mean in PHP?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào