1

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I dont want to assign values one by one. that is too much time consuming.
Then use ArrayAccess with combination of magic method __get()
ok.. will keep that in mind. rating your answer +1 and will wait for more answers. I think this question is similar stackoverflow.com/questions/9453933/…

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.