Jump to content

not sure how to structure includes for interdependent classes


alexweber15

Recommended Posts

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

Link to comment
Share on other sites

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();
}

}

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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.