3

I want to create instances of objects using PDO. I know that there's plently of questions already but I have only found that it is possible to send an array of paramters to a constructor. To me it seems that the constructor can only accept an array as argument. However this would make the constructor less meaningful.

I want to create a class with my own getters and setters like this:

class MyClass {
    private propertyA
    private propertyB

    public __constructor($argA, $argB) {
        $this->setPropertyA($argA);
        $this->setPropertyB($argB);
    }

    public setPropertyA($arg) {
        $this->proprtyA = $arg;
    }
}

Is there an elgant way to create an instance of such a class using data from a database, preferably using PDO.

9
  • The very idea of instantiating classes right out of SQL query is against any elegance. Commented Mar 21, 2013 at 10:07
  • I've also conciderd creating repository classes and pass data from FETCH_ASSOC to my model class' constructor. Maybe this is better? Commented Mar 21, 2013 at 10:14
  • Why do you want to use data from the database to create objects? WHY??? Commented Mar 21, 2013 at 13:00
  • Why not? Say If I want to edit a row representing an instance of a class. I'd create a instance of that class with data returned from the database passed into its class constructor. That way I can edit the properties of that class using the class' getters and setters and then save it back to the database. We did something similar in class but using .NET and EF, they called it the "repository/service-pattern". I thought that I could skip the whole ORM thing in PHP and use SQL instead of LINQ since it's not that different in syntax. Commented Mar 21, 2013 at 16:09
  • 1
    These comments are very strange, do you just operate on dumb maps (arrays or stdClass objects) or result sets then?
    – Esailija
    Commented Mar 22, 2013 at 15:30

1 Answer 1

1

Freeballin' here:

$handle = new PDO("blahblahblah");
$statement = $handle->prepare("SELECT blahblah");
$statement->execute();
$object_params = $statement->fetch(PDO::FETCH_ASSOC);

$object = new Object($object_params);
// OR...
$object = new Object($object_params["col_1"], $object_params["col_2"], "etc.");

Alternatively:

class MyClass {
    private propertyA
    private propertyB

    public __constructor() {
        $handle = new PDO("blahblahblah");
        $statement = $handle->prepare("SELECT blahblah");
        $statement->execute();
        $object_params = $statement->fetch(PDO::FETCH_ASSOC);

        $this->setPropertyA($object_params[$argA_key]);
        $this->setPropertyB($object_params[$argB_key]);
    }

    public setPropertyA($arg) {
        $this->proprtyA = $arg;
    }
}

I don't know what you're looking for exactly. Could you comment?

4
  • Your first example looks nice! I was looking for an alternative to FETCH_CLASS since I dont't like how it handles properties. Commented Mar 22, 2013 at 16:17
  • @GabrielSmoljár Just out of curiosity, what don't you like about how it handles properties? FETCH_CLASS just matches up column names to property names and creates an array of objects for each row. I don't see what's so bad about that? Commented Mar 22, 2013 at 17:09
  • it's the fact that it bypasses my custom getters and setters. Commented Mar 22, 2013 at 19:28
  • yes so how to prevent it from bypassing the getters and setters ? I'm using a DAO pattern and I need to have a seperate class from the dao class.
    – NewBie1234
    Commented Mar 16, 2019 at 12:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.