centenial Posted April 21, 2007 Share Posted April 21, 2007 Hi, I have 2 different classes. 1) MySQL - class.mysql.php 2) Cart - class.cart.php The MySQL class allows me to efficiently write/execute queries. The cart class has functions like AddToCart, UpdateCart, and DeleteFromCart. However, to perform those functions I need to connect to the database and execute a query. What is the best way to 'include' the functionality contained in the MySQL class to the Cart class? Should I do a "class extend", or simply 'include' the class.mysql.php file and create a new instance of the database class in each function? Or could I create one instance of the MySQL class ($MYSQL = new MySQL;) at the top of the Cart class and would that apply to all the functions within the Cart class? I'd like to do this the "right way", so any help would definitely be appreciated! Link to comment https://forums.phpfreaks.com/topic/48064-oop-question/ Share on other sites More sharing options...
jchemie Posted April 21, 2007 Share Posted April 21, 2007 Well, The best way to use the mysql class and the cart class is extend the cart class to have the database functionallity something like class database { .. .. .. } class cart extends database { // the cart functionallity. } This would let you use the database and cart as a single class as well as the code seperation will still be there. Hope this helps. Jyot Link to comment https://forums.phpfreaks.com/topic/48064-oop-question/#findComment-234900 Share on other sites More sharing options...
boo_lolly Posted April 21, 2007 Share Posted April 21, 2007 there's an OOP child-forum here. you can also look in my signature under oop for more help. Link to comment https://forums.phpfreaks.com/topic/48064-oop-question/#findComment-234916 Share on other sites More sharing options...
utexas_pjm Posted April 22, 2007 Share Posted April 22, 2007 The best way to use the mysql class and the cart class is extend the cart class to have the database functionallity something like I strongly disagree with this statement. <?php class Cart { var $db; function Cart(&$db) { $this->db =& $db; } function addToCart($item) { $this->db->query(); } } $db = new Database(); $cart = new Cart($db); ?> BEst, Patrick Link to comment https://forums.phpfreaks.com/topic/48064-oop-question/#findComment-235011 Share on other sites More sharing options...
Barand Posted April 22, 2007 Share Posted April 22, 2007 I strongly agree with utexas_pjm. Link to comment https://forums.phpfreaks.com/topic/48064-oop-question/#findComment-235098 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.