ohdang888 Posted February 10, 2012 Share Posted February 10, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/256845-mysql-wrapper-as-a-object-var/ Share on other sites More sharing options...
KevinM1 Posted February 10, 2012 Share Posted February 10, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256845-mysql-wrapper-as-a-object-var/#findComment-1316780 Share on other sites More sharing options...
scootstah Posted February 10, 2012 Share Posted February 10, 2012 If the API class inherits the MySQL wrapper, that is to say the API class is a subclass of the MySQL wrapper. I don't think that's what you want. Quote Link to comment https://forums.phpfreaks.com/topic/256845-mysql-wrapper-as-a-object-var/#findComment-1316781 Share on other sites More sharing options...
ohdang888 Posted February 10, 2012 Author Share Posted February 10, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/256845-mysql-wrapper-as-a-object-var/#findComment-1316784 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.