Current Location: Home> Latest Articles> PDOStatement::fetchObject: How to use this function to return a custom object

PDOStatement::fetchObject: How to use this function to return a custom object

gitbox 2025-05-11

In PHP, PDO (PHP Data Objects) is a lightweight database abstraction layer for accessing databases. Using PDO can easily execute various SQL queries and supports a variety of database systems such as MySQL, PostgreSQL, SQLite, etc. The PDOStatement::fetchObject function is a very powerful method provided by PDO. It can directly map query results into custom PHP objects.

What is PDOStatement::fetchObject ?

PDOStatement::fetchObject is a function that takes a row of records from the database query results and maps them into an object. Unlike the fetch method, fetchObject returns not an associative array or index array, but an object. By default, the returned object is of type stdClass , but we can map the query results to the custom object by passing in the custom class name.

Basic usage of PDOStatement::fetchObject

1. Simple example: Returning the stdClass object

First, use the basic PDO to connect to the database and execute a query, use fetchObject to get the query result and return a stdClass object:

 <?php
$pdo = new PDO('mysql:host=localhost;dbname=testdb', 'username', 'password');
$stmt = $pdo->query('SELECT * FROM users');
while ($row = $stmt->fetchObject()) {
    echo $row->name . "\n";  // Suppose there is one in the query table name Fields
}
?>

In this example, $row will be a stdClass object, you can access fields of database records just like you can access the object's properties.

2. Map to custom objects

If you want to map the query results to a specific class object, you can pass the name of the class in the fetchObject . Here is how to use a custom class to handle query results:

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

    public function __construct($id, $name, $email) {
        $this->id = $id;
        $this->name = $name;
        $this->email = $email;
    }
}

$pdo = new PDO('mysql:host=localhost;dbname=testdb', 'username', 'password');
$stmt = $pdo->query('SELECT id, name, email FROM users');
while ($user = $stmt->fetchObject('User')) {
    echo "ID: " . $user->id . " Name: " . $user->name . " Email: " . $user->email . "\n";
}
?>

In this example, fetchObject('User') maps the query result to an object of the User class. Each row of data creates a User object, and you can access the data directly through the object properties.

3. Initialize data using constructor

By default, fetchObject will directly use fields in the query result to set the object's properties. If your class has a constructor, PDO ignores the constructor parameters. In this case, you can use the PDOStatement::setFetchMode method to solve this problem. For example, if you want to initialize an object through a constructor, you can use the following method:

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

    public function __construct($id, $name, $email) {
        $this->id = $id;
        $this->name = $name;
        $this->email = $email;
    }
}

$pdo = new PDO('mysql:host=localhost;dbname=testdb', 'username', 'password');
$stmt = $pdo->query('SELECT id, name, email FROM users');
$stmt->setFetchMode(PDO::FETCH_CLASS, 'User');
while ($user = $stmt->fetch()) {
    echo "ID: " . $user->id . " Name: " . $user->name . " Email: " . $user->email . "\n";
}
?>

By setting PDO::FETCH_CLASS , we tell PDO that when fetching data, we should use a custom constructor to initialize the object.

Summarize

  • PDOStatement::fetchObject allows us to easily map query results into an object.

  • By default, fetchObject returns a stdClass object, but we can also map the query results to a custom PHP class object by specifying the class name.

  • If you need to initialize the object through a constructor, you can use the setFetchMode method to specify the PDO::FETCH_CLASS mode.

With PDOStatement::fetchObject , you can more conveniently manipulate database query results and process data in an object-oriented manner.