Jump to content

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.