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.
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?