I had a rather short question, doesn't OOP Programming make you increase the amount of executed queries?

class Guild{
	private $members[];
	private $sName;
	private $iLevel;
	
	function __construct($guildId)
	{
		// get all information from db using the guildid
		// also get all memberid's from the db and pass these trough
		// to the constructor of the Personclass.
	}
}

class Person{
	private $sName;
	
	function __construct($userid)
	{
		// now get my information from the db using my userid.
	}
}

And is there a way to do this diffrent?

Is it common by the way to use the constructor to get the information from the database and use the destruct it to save the modified fields?

Kind regards

Member Avatar for melissagirl

Whether it had more queries or not would depend on how each piece of code is written. Intrinsically, it does not make more calls. If I were to do it different, I would probably make a separate function that makes all the mysql calls and call that from the constructor so that the constructor itself isn't too messy.

I'm not sure if it's common or not, but that certainly is one way of doing it. When I wrote a game with an OOP php backend, I would load the minimal data at startup upon initialization and then pull up additional information as needed. That way you don't need grab *everything* if not all is needed.

This really has nothing to do with OOP particularly, mostly it has to do with your setup.

The way I would do it to minimize queries would be to select all data with users that have that guild id. Then I would pass the user data into the person class using the constructor or individual methods. That way you are not querying the database for every user again.

Yes, I think its pretty common to use the constructor to get info and destructor to save/update it. Its something I use to cut down on the number of queries.

Thank you for this information and the quick response, it was certainly useful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.