9.3. Code Example

For the mastering process of a CD, the application needs to compile the required information into the CD object. This object will be passed on to an external vendor, who will process the actual CD creation. The CD object needs to contain the title, the band name, and the track list.

This simple CD class contains methods to add the title, band, and track list:

class CD
{
    public $title = '';
    public $band = '';
    public $tracks = array();

    public function __construct()
    {}

    public function setTitle($title)
    {
        $this->title = $title;
    }

    public function setBand($band)
{
        $this->band = $band;
    }

    public function addTrack($track)
    {
        $this->tracks[] = $track;
    }
}

In order to make a complete CD object, the process is always the same. Create an instance of the CD class, then add the title, band name, and track list:

$title = 'Waste of a Rib';
$band = 'Never Again';
$tracksFromExternalSource = array('What It Means', 'Brrr', 'Goodbye');

$cd = new CD();
$cd->setTitle($title);
$cd->setBand($band);
foreach ($tracksFromExternalSource as $track) {
    $cd->addTrack($track);
}

Some artists are now releasing additional content on their CDs that can be used on the computer. These CDs are called enhanced CDs. The first track written to the disc is a data track. The mastering software recognizes the data track by its label of 'DATA TRACK' and will create the CD accordingly.

The enhancedCD class is similar to the regular CD class. It has the same public methods. However, it does add the first track ...

Get Professional PHP Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.