fetchObject

Summary: in this tutorial, you’ll learn how to use the fetchObject() method to fetch the next row from a result set and returns it as an object.

Introduction to the fetchObject() method

Suppose that you have a pulishers table:

CREATE TABLE IF NOT EXISTS publishers (
    publisher_id INT AUTO_INCREMENT,
    name VARCHAR(255) NOT NULL,
    PRIMARY KEY (publisher_id)
);Code language: PHP (php)

and a Publisher class:

<?php

class Publisher
{
    private $publisher_id;
    private $name;
}Code language: PHP (php)

To fetch each row from the publishers table and returns it as a Publisher object, you use the fetchObject() method:

public function fetchObject(
    string|null $class = "stdClass", 
    array $constructorArgs = []
): object|falseCode language: PHP (php)

The fetchObject() is a method of the PDOStatement class. It fetches the next row from the result set associated with a PDOStatement object and returns an object of a class. If the fetchObject() method fails, it’ll return false instead.

The fetchObject() method has two parameters:

  • $class specifies the class of the object to return. If you omit it, the fetchObject() method will return an instance of the stdClass class.
  • constructorArgs is an array that specifies the arguments passed to the constructor of the $class.

The fetchObject() maps the columns of the row with the properties of the object with the following rules:

  • First, assign the column value to the property with the same name.
  • Second, call the __set() magic method if a column doesn’t have a property in the class with the same name. If the classs has no __set() magic method, create a public property with the value derived from the column.

PHP fetchobject() method example

The following example shows how to use the fetchObject() method to fetch a row from the publishers table and returns a Publisher object:

<?php

class Publisher
{
    private $publisher_id;
    private $name;
}

// connect to the database
$pdo = require 'connect.php';

// execute the query
$sql = 'SELECT publisher_id, name 
        FROM publishers 
        WHERE publisher_id=:publisher_id';

$statement = $pdo->prepare($sql);
$statement->execute([':publisher_id' => 1]);

// fetch the row into the Publisher object
$publisher = $statement->fetchObject('Publisher');

var_dump($publisher);Code language: PHP (php)

Output:

object(Publisher)#3 (2) { 
    ["publisher_id":"Publisher":private]=> string(1) "1" 
    ["name":"Publisher":private]=> string(21) "McGraw-Hill Education" 
}Code language: PHP (php)

How it works.

First, define a Publisher class with two properties publisher_id and name:

class Publisher
{
    private $publisher_id;

    private $name;
}Code language: PHP (php)

Second, connect to the bookdb database using the connect.php object:

$pdo = require 'connect.php';Code language: PHP (php)

Third, execute a prepared statement that selects a publisher with id 1 from the publishers table:

// execute the query
$sql = 'SELECT publisher_id, name 
        FROM publishers 
        WHERE publisher_id=:publisher_id';

$statement = $pdo->prepare($sql);
$statement->execute([':publisher_id' => 1]);Code language: PHP (php)

Finally, return a Publisher object:

$publisher = $statement->fetchObject('Publisher');Code language: PHP (php)

Note that if the Publisher class has a namespace, you need to pass a fully qualified class name. For example:

<?php

namespace phptutorial;

class Publisher
{
    private $publisher_id;
    private $name;
}

// connect to the database
$pdo = require 'connect.php';

// execute the query
$sql = 'SELECT publisher_id, name 
        FROM publishers 
        WHERE publisher_id=:publisher_id';

$statement = $pdo->prepare($sql);
$statement->execute([':publisher_id' => 1]);

// fetch the row into the Publisher object
$publisher = $statement->fetchObject('phptutorial\Publisher');

var_dump($publisher);Code language: PHP (php)

Note the first line that defines the namespace phptutorial:

namespace phptutorial;
Code language: PHP (php)

And the line that uses the fetchObject() method:

$publisher = $statement->fetchObject('phptutorial\Publisher');
Code language: PHP (php)

Summary

  • Use the fetchObject() method to fetch the next row from a result set and returns it as an object of a class.
Did you find this tutorial useful?