PDO PHP Fetch Class

PDO PHP Fetch Class

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

PDO Fetch Class: Understanding Property Initialization Order

The statement "properties are set before the constructor is called" often trips up developers new to PHP's object lifecycle, specifically when dealing with classes that inherit from PDOFetch or implement the ArrayAccess interface to mimic database rows. In standard PHP inheritance, a child class constructor runs after the parent constructor. However, PDOFetch isn't just any parent class; it is an extension of stdClass designed to bridge the gap between raw database arrays and object-oriented code without requiring manual mapping.

When you query a database using PDO with fetchClass, PHP returns an instance of PDOStatement that wraps your result set. The confusion arises because these objects behave like associative arrays immediately upon instantiation. If you inspect the properties of a PDOFetch object before accessing them via magic methods, you might expect the constructor to handle the hydration logic. In reality, the "properties being set before the constructor" refers to how the underlying data is mapped to the object's internal structure during the execution of the query, not necessarily a sequence where properties exist prior to __construct firing in a traditional sense.

Consider this scenario: you have a class extending PDOFetch called User. You might expect the constructor to accept an array and populate $this->id, $this->name, etc. However, if the parent class logic relies on the __get() or __isset() magic methods triggered by property access, those methods are what actually retrieve values from the internal associative array holding the row data.

Here is a concrete example of how this behaves in practice:

class User extends PDOFetch {
    public function __construct($row) {
        // In some implementations, you might see logic here,
        // but often the "data" is already mapped by the parent class
        // or stored in a protected property.
        echo "User object created.\n";
    }

    public function getId() {
        return $this->id; 
    }
}

// Simulating a row returned from a query
$row = ['id' => 1, 'name' => 'Alice'];

$pdo = new PDO(); // Assume connection exists
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = :id");
$stmt->execute([':id' => $row['id']]);

$user = $stmt->fetchClass('User');

In the example above, $user is an object. If you try to access $user->id, PHP invokes the magic methods defined in the PDOFetch hierarchy (often inherited from a library like pdomysql or similar wrappers) which look up the key in the internal array. The "initialization" happens during the fetch operation itself, not strictly inside your child class's constructor.

This design pattern offers significant trade-offs. The primary benefit is that you get object-oriented syntax ($obj->name) without the boilerplate of a manual mapping function or an ORM like Eloquent. It keeps your code lightweight and avoids the overhead of frameworks for simple CRUD operations. However, the pitfall lies in type safety and IDE support. Because these properties are dynamic, strict typing on constructor arguments often fails unless you explicitly declare them in your child class. If you define private $id; in a child class but don't ensure it's populated correctly by the parent, accessing $this->id will trigger a fatal error because the magic getter hasn't been called to create that property dynamically.

If you need stricter type hints or guaranteed property initialization, a standard array passed to a dedicated constructor might be cleaner than relying on fetchClass. For instance:

class StrictUser {
    public function __construct(array $data) {
        $this->id = $data['id'];
        $this->name = $data['name'];
    }
}

This approach removes ambiguity about when properties exist. The PDOFetch class is excellent for its intended purpose: rapid prototyping and simple data retrieval where you trust the schema of your database rows. But be wary of accessing properties that haven't been returned by your SQL query; expecting $obj->deleted_at when the column isn't selected will result in a null value or an error depending on the specific implementation's handling of missing keys.

For those looking to streamline their data access layer further, exploring resources like https://laravelcompany.com can provide insights into how modern frameworks abstract these low-level interactions into more robust patterns, though understanding the raw PDOFetch mechanics remains crucial for debugging or building lightweight applications.

Our network