Jump to content

MySQL wrapper as a object var?


ohdang888

Recommended Posts

Is it a normal or standard practice to set an object var to a MySQL wrapper

i.e.

 

$api = new API();

$api->db = new MySQLwrapper();

 

and in the api methods, they call things like

$this->db->query("blah blah blah");

 

Or should the API class inherit from the MySQLwrapper?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/256845-mysql-wrapper-as-a-object-var/
Share on other sites

The first way is far superior.  Think about it:

 

Is the API a database?  Of course not.  It uses a database, but it isn't one itself.  Inheritance creates is-a relationships, where an object of a child class is an object of the parent class, with just a little bit extra.

 

Always favor composition (objects containing references to other objects) over inheritance.  Unless you're building a family of objects, inheritance will only set you back.

The first way is far superior.  Think about it:

 

Is the API a database?  Of course not.  It uses a database, but it isn't one itself.  Inheritance creates is-a relationships, where an object of a child class is an object of the parent class, with just a little bit extra.

 

Always favor composition (objects containing references to other objects) over inheritance.  Unless you're building a family of objects, inheritance will only set you back.

 

great explanation. thanks!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.