11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

www.it-ebooks.infoCHAPTER 6 • OBJECT-ORIENTED <strong>PHP</strong>PrivateMethods marked as private are available for use only within the originating class and cannot be called bythe instantiated object, nor by any of the originating class’s child classes. Methods solely intended to behelpers for other methods located within the class should be marked as private. For example, consider amethod called validateCardNumber() that is used to determine the syntactical validity of a patron’slibrary 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 directly from an instantiated object result in a fatal error.ProtectedClass methods marked as protected are available only to the originating class and its child classes. Suchmethods 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 employeeidentification number (EIN) passed in as an argument to the class instantiator. You would then verifythis EIN for syntactical correctness using the verifyEIN() method. Because this method is intended foruse only by other methods within the class and could potentially be useful to classes derived fromEmployee, it should be declared as protected:}protected function verifyEIN($ein){return TRUE;}}$employee = new Employee("123-45-6789");Attempts to call verifyEIN() from outside of the class or from any child classes will result in a fatalerror because of its protected scope status.147

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!