Jump to content

Class organization


JakeTheSnake3.0

Recommended Posts

I've made several classes which pertain to product information.  They are as such:

 

1) class.Product.php

2) class.WineProduct.php

3) class.ProductReview.php

4) class.ProductAward.php

 

Currently class.WineProduct.php extends class.Product.php; ProductReview and ProductAward are just included in the Product class - this I know is wrong because when I try to run a function from within that class I get the error:

 

Cannot access parent:: when current class scope has no parent

 

Here's a sample of the offending code:

 

$wine = new WineProduct();
$wine->AddAward('Gold');
// This is the bad line:
foreach($wine->awards as $awd) $awd->SaveToDB();

 

The SaveToDB function uses parent::barcode in an SQL query.  The barcode is a property of the Product class.  I know this has something to do with the include() statements...but what SHOULD I have done?

Link to comment
Share on other sites

I actually am extending the product class through wineproduct, it's just that the product class includes 'productaward' and 'productreview'.

 

Sample code for class.Product.php

include('class.ProductReview.php');
class Product {
    public $reviews;
    public function AddReview($info) {
	$this->reviews[] = new ProductReview($info);
}
}

 

Sample code for class.ProductReview.php

class ProductReview {
    public $info;
    public function SaveToDB() {
            // This is where I generate the SQL query.  Seeing as how the Product class contains the barcode # I need for it,
            // I figured I'd use parent::barcode to get it.  It doesn't work.
            // So in my main code, I'd use $wine->reviews[0]->SaveToDB
            // With the structure I have now, I'd have to pass the barcode as an argument to the SaveToDB function.  If there's a way of avoiding having to do that, I'd prefer it.
    }
}

Link to comment
Share on other sites

$product = new Product();
$product->addReview(1, $auth->getIdentity(), 'B. E. autiful.');
$product->addAward(1, 'Super-Awesome-Comes-Highly-Recommended-stuff');

 

class Product
{
    public function addReview($productID, $userID, $review) {
        $review = new ProductReview();
        $review->product_id  = $productID;
        $review->reviewed_by = $userID;
        $review->message     = $review;
        $review->save();
    }
}

class ProductReview
{
    // code
}

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.