Jump to content

OOP question


centenial

Recommended Posts

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

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

    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

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.