Skip to content
Advertisement

PHP – constructor with SQL query [closed]

Option 1: query the data first and then pass the data to the constructor

Option 2: use the constructor to query the data and then fill the properties


Option 1 Example

JavaScript

Option 2 Example

JavaScript

NOTES

I am perfectly aware that mysql_query is deprecated. Please resist the overwhelming urge to tell me that. These are the reqs for the project.

I am asking if Option 2 is bad practice or if there are any foreseen predicaments that are overwhelmingly known in the object oriented space. It seems to be the cleaner option to me.

Advertisement

Answer

Show this to whoever is in charge. enter image description here


But anyways back to your question.

As much as I hate to answer like this, I think option 2 definitely makes the code cleaner, but you can also make it even more clean, by creating a method in the class that returns what you’re looking for.

JavaScript

On your other file you can then do this

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