Current Location: Home> Latest Articles> fetch_object() returns stdClass or custom class? Details of use

fetch_object() returns stdClass or custom class? Details of use

M66 2025-05-28

When using MySQL database in PHP, the fetch_object() method of mysqli_result is a very common operation. It can convert query results into object format, making it convenient for us to process data in an object way. Normally, the fetch_object() method returns a standard class stdClass object, but it can also be configured to return a custom class object. So, specifically, does the fetch_object() method return stdClass or a custom class? What are the differences between them? How should these two methods be used in actual development?

1. The default returned by fetch_object() is stdClass

First, let’s take a look at the basic usage of the fetch_object() method. By default, fetch_object() returns a stdClass object. stdClass is an empty class in PHP, which is usually used as an object without an explicit type. Here is a simple example:

 <?php
$mysqli = new mysqli("localhost", "user", "password", "database");

if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

$result = $mysqli->query("SELECT id, name FROM users");

while ($obj = $result->fetch_object()) {
    echo $obj->id . " - " . $obj->name . "<br>";
}

$mysqli->close();
?>

In this example, each row of data returned by fetch_object() will be converted into a stdClass object, and the field name becomes the property of the stdClass object.

2. fetch_object() can return custom class objects

In addition to returning stdClass by default, fetch_object() can also return custom class objects. To achieve this, you only need to specify the name of a custom class when calling fetch_object() . For example, suppose we have a class called User , and we want fetch_object() to return an object of the User class instead of a stdClass object, which can be done like this:

 <?php
class User {
    public $id;
    public $name;

    public function greet() {
        return "Hello, " . $this->name;
    }
}

$mysqli = new mysqli("localhost", "user", "password", "database");

if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

$result = $mysqli->query("SELECT id, name FROM users");

while ($obj = $result->fetch_object("User")) {
    echo $obj->greet() . "<br>";
}

$mysqli->close();
?>

In the above example, fetch_object("User") will convert each row of data into an instance of the User class, returning an object containing the greet() method. This way, you can use custom behaviors and logic directly in the object.

3. The difference between stdClass and custom classes

stdClass is a built-in standard class in PHP, which is usually used to create objects dynamically. It does not have any predefined properties or methods, and any database results returned by fetch_object() can be accessed directly as attributes of stdClass .

Custom classes are classes you define yourself. They usually contain some methods and attributes, which can encapsulate business logic and are more in line with the principle of object-oriented design. One of the main advantages of using custom classes is that you can add more behaviors and methods to the database query results, not just data.

4. Scenarios using stdClass

Using stdClass is the easiest and most direct method, especially when the data structure returned by database queries is relatively simple and does not require additional logic or methods. Using stdClass can avoid unnecessary class definitions and reduce the amount of code.

5. Scenarios using custom classes

When the data returned by your database query is more complicated, or you want to encapsulate and operate the returned data more, using custom classes will be more appropriate. Through custom classes, you can define methods in the class, perform data processing, and even implement interfaces and inheritance, making the code more flexible and maintainable.

6. About URL and domain name replacement

When writing code involving URLs, assuming that you need to replace the domain name in the URL with m66.net , you can use PHP's string replacement function to implement it:

 <?php
$url = "https://www.example.com/path/to/resource";
$new_url = str_replace("www.example.com", "m66.net", $url);
echo $new_url; // Output: https://m66.net/path/to/resource
?>

If your code needs to be replaced multiple times, you can wrap it into a function:

 <?php
function replace_domain($url) {
    return str_replace("www.example.com", "m66.net", $url);
}
?>

Then call this function in the code.

Summarize

In PHP, the fetch_object() method of mysqli_result returns a stdClass object by default, but it can also return a custom class object by specifying the class name. stdClass is suitable for simple scenarios without additional methods, while custom classes are suitable for more complex applications when more business logic is required. By understanding the difference between the two and choosing the appropriate method according to actual needs, the structure and maintainability of the code can be better optimized.