Skip to content
Advertisement

How to use PDO::FETCH_CLASS with __set() method?

I want to create an object right after fetching data from my database.

For that I’m using PDO::FETCH_CLASS and a rewritten __set() method that I’ve put in a trait.

However, I can’t figure out why I have an error mentionning : “Fatal error: Uncaught ArgumentCountError: Too few arguments to function Banner::__construct(), 0 passed and exactly 1 expected in D:Logicielswamp64wwwprojet-4writer-blogentityBanner.php on line 34”

Here is my Banner class (setters and getters omitted) :

JavaScript

My magical method __set() is in the following Trait :

JavaScript

And here is the beggining of my BannerManager where I fetch my data :

JavaScript

Advertisement

Answer

Your actual question here is How to use PDO::FETCH_CLASS with a class that requires constructor parameters as that’s what the error message says.

And the answer is setFetchMode’s third parameter..

JavaScript

should do the trick, providing an empty array for the constructor, thus letting your code reach the point where __set() is actually called.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement