alexweber15 Posted April 21, 2008 Share Posted April 21, 2008 Hi all, I'm working on a simple shopping cart implementation for learning purposes. I have a Product Class and a Shopping Cart class, each in a seperate .php file Naturally, the Shopping Cart Class is a collection of Product Objects. Now in the Shopping Cart Class at some point I'll need to call Product Object methods, how does this work as far as includes go? Where do I include the Product class in my Shopping Cart code? Or by including both classes in my index this isn't necessary? Thanks! Alex Quote Link to comment https://forums.phpfreaks.com/topic/102195-not-sure-how-to-structure-includes-for-interdependent-classes/ Share on other sites More sharing options...
Northern Flame Posted April 22, 2008 Share Posted April 22, 2008 do you mean you will need to use a function from your products class inside your shopping cart class? if so, yes, you will need to include the products class into the shopping cart function like so: <?php include('products-class.php'); class shopping{ function something(){ products::someFunction(); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/102195-not-sure-how-to-structure-includes-for-interdependent-classes/#findComment-523663 Share on other sites More sharing options...
Cobby Posted April 22, 2008 Share Posted April 22, 2008 do you mean you will need to use a function from your products class inside your shopping cart class? if so, yes, you will need to include the products class into the shopping cart function like so: <?php include('products-class.php'); class shopping{ function something(){ products::someFunction(); } } ?> That would involve the Products class being static. Instead consider this: <?php require_once 'Products.php'; class ShoppingCart { protected $_products; public function __construct(){ $this->_products = new Products(); } public function getCategoryProducts($cid){ $products; // load Product IDs from given Category ID foreach($products as $pid){ $category[] = $this->_products->getProduct($pid); } return $category; } } ?> Depending on how you have set up the products class, you might consider simply extending, but the above would be my preffered method. Quote Link to comment https://forums.phpfreaks.com/topic/102195-not-sure-how-to-structure-includes-for-interdependent-classes/#findComment-523824 Share on other sites More sharing options...
yarsu Posted April 22, 2008 Share Posted April 22, 2008 why don't you indent the produt class in shopping Quote Link to comment https://forums.phpfreaks.com/topic/102195-not-sure-how-to-structure-includes-for-interdependent-classes/#findComment-523826 Share on other sites More sharing options...
moon 111 Posted April 22, 2008 Share Posted April 22, 2008 Include them both. I would suggest puting both in the same file. Quote Link to comment https://forums.phpfreaks.com/topic/102195-not-sure-how-to-structure-includes-for-interdependent-classes/#findComment-524213 Share on other sites More sharing options...
aschk Posted April 23, 2008 Share Posted April 23, 2008 Keep them seperate. Have a Product.php and a Cart.php (or ShoppingCart.php), and consider using an autoloader (__autoload() override, or Zend autoloader). I wouldn't describe a shopping cart as a composite of products exactly, although i'm unsure exactly how I might model it (as shopping cart for example wouldn't have a price attribute). However I would imagine that you can "add" products to the cart. Something like the following for example: <?php /** * Product (should probably be abstract). * */ class Product { private $name; private $price; public function __construct($name = '', $price = 0.00){ $this->name = $name; $this->price = $price; } public function getPrice(){ return $this->price; } public function getName(){ return $this->name; } } /** * Cart class. * */ class Cart { private $products; public function __construct(){ $this->products = array(); } public function addProduct(Product $product){ array_push($this->products, $product); } public function getTotal(){ $total = 0.00; foreach($this->products as $product){ $total += $product->getPrice(); } return $total; } } $cart = new Cart(); $cart->addProduct( new Product('teddy bear', 2.99) ); $cart->addProduct( new Product('train set', 5.99) ); echo $cart->getTotal(); ?> Note: in the above i have put the Product and Cart together, however these SHOULD be in separate files as I described earlier, and the test example at the bottom should be in a controller (action). Quote Link to comment https://forums.phpfreaks.com/topic/102195-not-sure-how-to-structure-includes-for-interdependent-classes/#findComment-524713 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.