picokojama 1 Posted July 31, 2013 Report Share Posted July 31, 2013 Hi everyone, I have something like more advanced problem, so I hope someone will help. No need for looking for variables and functions matches, I have good editor, and if there is any, it is because I am translating code to english. Here I have class DBobject, which contains all common database methods, but my focus is on two: public static function find_by_sql($sql) { global $pdo; // pdo connection works fine $query = $pdo->query($sql); $object_array = array(); while($row = $query->fetchAll()) { $object_array[] = static::instantiate($row); } return $object_array; } public static function instantiate($row) { $class = get_called_class(); $object = new $class; // because it can be used for users, pages... I don't wont to repeat my code foreach ($row as $attribute => $value) { $object->$attribute = $value; } return $object; } It is parent class of many classes, and I am looking in the class Comments protected static $table_name='komentari';protected static $db_fields=array(// all of my fields are here);public $id;public $user_id;// and all of database fields as variables And now, when I want to call it... $comment = Comment::find_by_sql(// here I select user based upon id); // this should return object with attributes from my database (because I returned object in instantiate function), am I right?echo $comment->content; // I exept here to echo lorem ipsum dolor sit amet.... And I get error that I called attribute of non-object $comment, aldo I returned the object in instantiate method Quote Link to post Share on other sites
justsomeguy 1,135 Posted July 31, 2013 Report Share Posted July 31, 2013 It doesn't return an object, it returns an array of objects. Each object is one row from the result. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.