Prev | Current Page 247 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

Methods solely intended to be helpers for other methods located within
the class should be marked as private. For example, consider a method, called
validateCardNumber(), used to determine the syntactical validity of a patron??™s library
card number. Although this method would certainly prove useful for satisfying a
number of tasks, such as creating patrons and self-checkout, the function has no use
when executed alone. Therefore, validateCardNumber() should be marked as private,
like this:
private function validateCardNumber($number)
{
if (! ereg('^([0-9]{4})-([0-9]{3})-([0-9]{2})') ) return FALSE;
else return TRUE;
}
Attempts to call this method from an instantiated object result in a fatal error.
Protected
Class methods marked as protected are available only to the originating class and its
subclasses. Such methods might be used for helping the class or subclass perform
internal computations. For example, before retrieving information about a particular
staff member, you might want to verify the employee identification number (EIN)
passed in as an argument to the class instantiator. You would then verify this EIN for
syntactical correctness using the verifyEIN() method. Because this method is intended
180 CHAPTER 6 ?–  O BJECT-ORIENTED PHP
for use only by other methods within the class and could potentially be useful to classes
derived from Employee, it should be declared as protected:
class Employee
{
private $ein;
function __construct($ein)
{
if ($this->verifyEIN($ein)) {
echo "EIN verified.


Pages:
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259