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
https://forums.phpfreaks.com/topic/225227-class-organization/
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
https://forums.phpfreaks.com/topic/225227-class-organization/#findComment-1163226
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
https://forums.phpfreaks.com/topic/225227-class-organization/#findComment-1163270
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.