Object Oriented PHP: Part 7

In case you haven't been reading, here is the series so far:

Getters and Setters

How that our classes are getting more secure we should consider preventing direct access to properties. To that end, we can set getter and setter methods that will, well, get and set the data members.

To review, here are our classes so far:


class SalesItem {\n
private $brand;\n
private $name;\n
private $price = 0;\n
\n
protected function __construct( $brand, $name, $price ) {\n
$this -> brand = $brand;\n
$this -> name = $name;\n
$this -> price = $price;\n
}\n
\n
protected function getSummary() {\n
$summary = "{$this -> brand}" . " {$this -> name}";\n
return $summary;\n
}\n
}\n
\n
class MP3Item extends SalesItem {\n
private $bitrate;\n
\n
public function __construct( $brand, $name, $price, $bitrate ) {\n
parent::__construct( $brand, $name, $price );\n
$this -> rpm = $rpm;\n
}\n
\n
public function getSummary() {\n
$summary = parent::getSummary();\n
$summary .= ", bitrate: {$this -> bitrate}";\n
return $summary;\n
}\n
\n
}\n
class LPItem extends SalesItem {\n
private $rpm;\n
\n
public function __construct( $brand, $name, $price, $bitrate ) {\n
parent::__construct( $brand, $name, $price );\n
$this -> rpm = $rpm;\n
}\n
\n
public function getSummary() {\n
$summary = parent::getSummary();\n
$summary .= ", speed: {$this -> rpm}rpm";\n
return $summary;\n
}\n
\n
}

Now, since our data members are all private, there are cases where we might want to be able to overwrite them, that's where getter and setter methods come in handy.


class SalesItem {\n
private $brand;\n
private $name;\n
private $price = 0;\n
\n
protected function __construct( $brand, $name, $price ) {\n
$this -> brand = $brand;\n
$this -> name = $name;\n
$this -> price = $price;\n
}\n
\n
protected function getSummary() {\n
$summary = "{$this -> brand}" . " {$this -> name}";\n
return $summary;\n
}\n
}\n
\n
class MP3Item extends SalesItem {\n
private $bitrate;\n
\n
public function __construct( $brand, $name, $price, $bitrate ) {\n
parent::__construct( $brand, $name, $price );\n
$this -> rpm = $rpm;\n
}\n
\n
public function getSummary() {\n
$summary = parent::getSummary();\n
$summary .= ", bitrate: {$this -> bitrate}";\n
return $summary;\n
}\n
\n
public function getBrand() {
return $this -> brand;
}
\n
public function getName() {
return $this -> name;
}
\n
public function getPrice() {
return $this -> price;
}
\n
public function getBitrate() {
return $this -> bitrate;
}
\n
public function setPrice( $price ) {
$this -> price = $price;
}
\n
}\n
class LPItem extends SalesItem {\n
private $rpm;\n
\n
public function __construct( $brand, $name, $price, $bitrate ) {\n
parent::__construct( $brand, $name, $price );\n
$this -> rpm = $rpm;\n
}\n
\n
public function getSummary() {\n
$summary = parent::getSummary();\n
$summary .= ", speed: {$this -> rpm}rpm";\n
return $summary;\n
}\n
\n
public function getBrand() {
return $this -> brand;
}
\n
public function getName() {
return $this -> name;
}
\n
public function getPrice() {
return $this -> price;
}
\n
public function getRPM() {
return $this -> rpm;
}
\n
public function setPrice( $price ) {
$this -> price = $price;
}
\n
}

Using getters and setters is easy,


$mymp3 = new MP3Item( "Hieroglyphics", "At the Helm", 0.99, 256 );\n
// Let's change the price...\n
$mymp3 -> price = setPrice( 0.89 ); // We can give them a discount\n
// Let's get the Bitrate...\n
print $mymp3 -> getBitrate(); // Prints 256

So there you go, the next couple posts will cover Abstract Classes, Interfaces and Static Methods and Properties.

User login

Contacting Rob

Y! roberthenrylowe
AIM roberthenrylowe
ICQ 249971225
email rhlowe [at] gmail dot com

Networking Links