tags:

views:

71

answers:

1

I'm trying to make a class that takes some unspecified data from a database query (passed to my class as an array) and turns it into a PHP object with properties that are created dynamically from the data passed to it.

Like so:

    class myLibrary_Item extends myLibrary
    {
            private function __construct($vars)
            {
                    foreach($vars as $var => $val)
                    {
                            $this->$var => $val;
                    }
            }

            private function __set($var, $val)
            {
                    $this->$var => $val;
            }

            private function __get($var)
            {
                    return $this->$var;
            }
    }  

$myArray = array(
    'firstName' => 'Joe',
    'lastName' => 'Carrington'
);

$myObject = new myLibrary_Item($myArray)
echo $myObject->firstName;
//Hopefully will output Joe

So, my question, is this a good idea at all? Am I missing the point of OOP here? Should I learn about PDO instead? It seems like this could be a big help, but I don't want to hammer out the bugs if this is going to bite me in the butt later.

A: 

I would say this is indeed redundant to PDOStatement->fetchObject.

php > $dbh = new PDO("mysql:host=localhost;dbname=test", "guest");
php > $stat = $dbh->prepare("select * from books");
php > $stat->execute();
php > while($row = $stat->fetchObject())
php > print_r($row);
stdClass Object
(
    [title] => Hitchhiker's Guide
    [author] => Douglas Adams
    [isbn] => 0345391802
    [publisher] => Del Rey
    [year] => 1995
    [summary] => Arthur Dent accidentally saves the world.
)
Matthew Flaschen