PHP Objects, Patterns and Practice (3rd edition)

(Barry) #1
CHAPTER 3 ■ OBJECT BASICS

$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;
}
}


There is nothing substantially new in this version of the ShopProduct family. I made all methods
explicitly public, and all properties are either private or protected. I added a number of accessor
methods to round things off.


Summary


This chapter covered a lot of ground, taking a class from an empty implementation through to a
fully featured inheritance hierarchy. You took in some design issues, particularly with regard to type and
inheritance. You saw PHP’s support for visibility and explored some of its uses. In the next chapter, I will
show you more of PHP’s object-oriented features.

Free download pdf