Object-Oriented PHP: Fields, Properties and Methods - Method Scopes
(Page 4 of 4 )
PHP supports six method scopes: public, private, protected, abstract, final, and static. The first five scopes are introduced in this section. The sixth, static, is introduced in the later section, “Static Members.”
Public
Public methods can be accessed from anywhere, at any time. You declare a public method by prefacing it with the keyword public, or by foregoing any prefacing whatsoever. The following example demonstrates both declaration practices, in addition to demonstrating how public methods can be called from outside the class:
<?php class Visitors { public function greetVisitor() { echo "Hello<br />"; } function sayGoodbye() { echo "Goodbye<br />"; } } Visitors::greetVisitor(); $visitor = new Visitors(); $visitor->sayGoodbye(); ?>
Methods marked as private are available for use only within the originating class and cannot be called by the instantiated object, nor by any of the originating class’s subclasses. 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 verify_ein() method. Because this method is intended for use only by other methods within the class, and could potentially be useful to classes derived from Staff, it should be declaredprotected:
<?php class Staff { private $ein; function __construct($ein) { if ($this->verify_ein($ein)) {
echo "EIN verified. Finish"; }
} protected function verify_ein($ein) { return TRUE; } } $employee = new Staff("123-45-6789"); ?>
Attempts to callverify_ein()from outside of the class will result in a fatal error, because of its protected scope status.
Abstract
Abstract methods are special in that they are declared only within a parent class but are implemented in child classes. Only classes declared as abstract can contain abstract methods. You might declare an abstract method if you’d like to define an application programming interface (API) that can later be used as a model for implementation. A developer would know that his particular implementation of that method should work provided that it meets all requirements as defined by the abstract method. Abstract methods are declared like this:
abstract function methodName();
Suppose that you wanted to create an abstractStaffclass, which would then serve as the base class for a variety of staff types (manager, clerk, cashier, and so on):
abstract class Staff { abstract function hire(); abstract function fire(); abstract function promote(); abstract demote(); }
This class could then be extended by the respective staffing classes, such as manager, clerk, and cashier. Chapter 7 expands upon this concept and looks much more deeply at abstract classes.
Final
Marking a method as final prevents it from being overridden by a subclass. A finalized method is declared like this:
class staff { ... final function getName() { ... } }
Attempts to later override a finalized method result in a fatal error. PHP supports six method scopes: public, private, protected, abstract, final, and static.
■Note The topics of class inheritance and the overriding of methods and fields are discussed in the next chapter.
Type Hinting
Type hinting is a feature new to PHP 5. Type hinting ensures that the object being passed to the method is indeed a member of the expected class. For example, it makes sense that only objects of class staff should be passed to the take_lunchbreak() method. Therefore, you can preface the method definition’s sole input parameter$employeewithstaff, enforcing this rule. An example follows:
private function take_lunchbreak (staff $employee) { ... }
Keep in mind that type hinting only works for objects. You can’t offer hints for types such as integers, floats, or strings.
Please check back next week for the conclusion to this article.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.