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:

php Copy
$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:

php Copy
$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

senior

Explain the Order of Precedence for Traits in PHP

senior

Compare MySQLi or PDO - what are the pros and cons?

junior

What is PDO in PHP?

Bình luận

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

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