I have created a class user which reflects row in a user table. There is one method populate in which I am fetching values from database based on primary key and try to store it in self object. I can do it by getting all variables and setting them one by one per field. Is there any way to do it using PDO::FETCH_CLASS or any other simple method ?
If I try to store it using $this = $stmt->fetch( PDO::FETCH_CLASS ) it gives Fatal error: Cannot re-assign $this in /var/www/html/php/classes.php on line 51 error.
I don't want to use ORM style tools and want to develop the class by my own.
Have you tried something like list($this->a, $this->b, $this->c) = array((...)), where array is (probably) your fetched result? Because you cannot assignt $this variable, only refernce it's fields and methods.
Another solution is to implement ArrayAccess interface and store data in underlying array.