Object Oriented PHP: Part 6

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

Class Access Modifiers

Hey, remember access modifiers? Well, we can use that same concept for more than just properties. Just like before, you have public, protected and private modifiers for classes.

  • Public methods can be accessed within it's class, by sub- and super classes and from outside the class
  • Protected methods can be accessed within it's class and by sub- and super classes
  • Private methods can only be accessed within it's class

Let's look at the classes we built last time:


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

We have a class, SalesItem, that is extended by both MP3Item and LPItem. Right now, all the properties and methods are either explicitly or implicitly public, lets fix that.

First, lets make all the properties private:


class SalesItem {\n
private $brand;\n
private $name;\n
private $price = 0;\n
\n
class MP3Item extends SalesItem {\n
private $bitrate;\n
\n
class LPItem extends SalesItem {\n
private $rpm;

(No, the classes aren't complete.) In their current state, you don't need direct access to the properties, we'll discuss how to set and get private and protected properties in a later post.

Next, let's set the methods to reflect how they are best used.


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
}

Here we made the methods in the super class protected, so they can only be accessed from within the class hierarchy. The methods in the child classes are all set to public as it is unlikely that a programmer would need direct access to SalesItem, but will need to access to the child classses.

Nothing is all that new here, we have made all the properties private and the methods either protected or public, depending on their context. Next time we will look ad getter and setter methods and further specify the privacy of the current methods.

User login

Contacting Rob

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

Networking Links