Jump to content

[SOLVED] What can I do with this class?


lopes_andre

Recommended Posts

Hi,

 

I'am reading the book "PHP Objects, Patterns and Practice" and one of the examples in the book is this one:

 

<?php


class ShopProductWriter {
public $products = array();

public function addProduct( ShopProduct $shopProduct ) {
	$this->products[] = $shopProduct;
}

public function write() {
	$str = "";
	foreach ( $this->products as $shopProduct ) {
	$str .= "{$shopProduct->title}: ";
	$str .= $shopProduct->getProducer();
	$str .= " ({$shopProduct->getPrice()})\n";
	}
        print $str;
}
}

##########################################

class ShopProduct {
private $title;
private $producerMainName;
private $producerFirstName;
protected $price;
private $discount = 0;

public function __construct( $title, $firstName, $mainName, $price ) {
	$this->title = $title;
	$this->producerFirstName = $firstName;
	$this->producerMainName = $mainName;
	$this->price = $price;
}

public function getProducerFirstName() {
	return $this->producerFirstName;
}

public function getProducerMainName() {
	return $this->producerMainName;
}

public function setDiscount( $num ) {
	$this->discount=$num;
}

public function getDiscount() {
	return $this->discount;
}

public function getTitle() {
	return $this->title;
}

public function getPrice() {
	return ($this->price - $this->discount);
}

public function getProducer() {
	return "{$this->producerFirstName}".
	" {$this->producerMainName}";
}

public function getSummaryLine() {
	$base = "{$this->title} ( {$this->producerMainName}, ";
	$base .= "{$this->producerFirstName} )";
	return $base;
}
}






class CdProduct extends ShopProduct {

private $playLength = 0;

public function __construct( $title, $firstName, $mainName, $price, $playLength ) {
	parent::__construct( $title, $firstName,
	$mainName, $price );
	$this->playLength = $playLength;
}

public function getPlayLength() {
	return $this->playLength;
}

public function getSummaryLine() {
	$base = parent::getSummaryLine();
	$base .= ": playing time - {$this->playLength}";
	return $base;
}
}




class BookProduct extends ShopProduct {

private $numPages = 0;

public function __construct( $title, $firstName, $mainName, $price, $numPages ) {
	parent::__construct( $title, $firstName, $mainName, $price );
	$this->numPages = $numPages;
}

public function getNumberOfPages() {
	return $this->numPages;
}

public function getSummaryLine() {
	$base = parent::getSummaryLine();
	$base .= ": page count - {$this->numPages}";
	return $base;
}

public function getPrice() {
	return $this->price;
}
}


$produto = new ShopProduct("AA", "BB", "CC", "0");
echo $produto->getTitle();




?>

 

My question is: What does the class "ShopProductWriter" and how use it. In the book I don't see any explanation on on to use this class.

 

 

Best Regards,

André.

Link to comment
Share on other sites

ShopProductWriter writes out your cart items. ShopProduct represents a product. You would use it like:

 

$spw = new ShopProductWriter();
$spw->addProduct(new ShopProduct('Product #1', 'Firstname', 'MainName', 'expensive'));
$spw->addProduct(new ShopProduct('Product #2', 'Firstname', 'MainName', 'expensive'));
$spw->addProduct(new ShopProduct('Product #3', 'Firstname', 'MainName', 'expensive'));
$spw->addProduct(new ShopProduct('Product #4', 'Firstname', 'MainName', 'expensive'));

print $spw->write();

 

I won't recommend using it on a production server (like it allows 'expensive' as a price..) though plus there are many things this class does not make advantage of (__toString(), ..).

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.