Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POD::FETCH_CLASS causes infinite loop while PDO::FETCH_BOTH does not

I've come across a difficult to track bug, but I'm not sure what is causing this bug. I have a class Property and I want to fetch one entry form the table property with a method named loadProperty(). This method is part from a singleton class (Registry).

public function loadProperty() {
    $this->load('model', 'property');
    $sth = $this->dbh->prepare("SELECT * FROM property WHERE subdomain = :subdomain LIMIT 1");
    $sth->setFetchMode(PDO::FETCH_CLASS, 'property');
    $data = array('subdomain' => $this->router->subdomain);

    try {
        $sth->execute($data);

        if ($sth->rowCount() == 1) {
            $this->property = $sth->fetch();
        } else {
            $this->property = null;
        }

    } catch (PDOException $exception) {
        // HANDLING EXCEPTION
    }
}

The first line of the method loads the model. It just looks for the class file and requires it with require_once.

All this works fine when I use PDO::FETCH_BOTH instead of PDO::FETCH_CLASS. My guess is that PDO is doing some things behind the scenes that I am not aware of, but that cause my loadProperty method to be called an infinite number of times.

What am I overlooking here?

like image 364
Bart Jacobs Avatar asked Feb 09 '26 22:02

Bart Jacobs


1 Answers

The infinite loop turned out to be caused by an error of my own - who'd've thought. By setting PDO's fetch mode to PDO::FETCH_CLASS, PDO attempts to instantiate an instance of Property, which one might expect. However, the model creates a reference to the Registry class in its constructor method, causing the constructor of the Registry class to be invoked which includes the loadProperty method shown above. The result is an infinite loop.

like image 134
Bart Jacobs Avatar answered Feb 12 '26 15:02

Bart Jacobs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!