PHP
  Home arrow PHP arrow Page 3 - Defining Public and Protected Methods ...
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Defining Public and Protected Methods with Member Visibility in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-06-11

    Table of Contents:
  • Defining Public and Protected Methods with Member Visibility in PHP 5
  • Calling methods of a class globally
  • Declaring and implementing protected methods within PHP 5 classes
  • Calling protected methods from a subclass

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Defining Public and Protected Methods with Member Visibility in PHP 5 - Declaring and implementing protected methods within PHP 5 classes


    (Page 3 of 4 )

    In accordance with the concepts that I expressed in the prior section, it would be pretty helpful to utilize the same data saving class that I created earlier to demonstrate how to declare and implement protected methods. Based on this simple schema, the next step will consist of listing the complete signature of the mentioned class, which now includes a couple of protected methods.

    That being said, here's how this sample class looks:


    // define 'DataSaver' class (methods are defined protected)


    class DataSaver{

    private $filePath;

    private $data;

    public function __construct($data,$filePath){

    if(!$data||strlen($data)>1024){

    throw new Exception('Invalid data for being saved to target file.');

    }

    if(!file_exists($filePath)){

    throw new Exception('Invalid target file.');

    }

    $this->data=$data;

    $this->filePath=$filePath;

    }

    // save data to target file

    public function save(){

    if(!$fp=fopen($this->filePath,'w')){

    throw new Exception('Error opening target file.');

    }

    if(!fwrite($fp,$this->data)){

    throw new Exception('Error writing data to target file.');

    }

    fclose($fp);

    }

    // get target file via an accessor

    protected function getFilePath(){

    return $this->filePath;

    }

    // get data via an accessor

    protected function getData(){

    return $this->data;

    }

    }


    This time, the sample "DataSaver" class now contains two accessors, called "getData" and "getFilePath()" respectively, that have been declared protected. Essentially, this condition means that they can only be accessed within the originating class and within all of its eventual subclasses.

    To demonstrate the veracity of this concept, below I coded yet another basic example to help you grasp the logic that stands behind using a pair of protected methods with the previous data saving class. Here's the corresponding code sample:


    try{

    // create new instance of 'DataSaver' class

    $dataSaver=new DataSaver('This string of data will be saved to a target file!','datafile.txt');

    // save data to target file

    $dataSaver->save();

    // call protected methods

    echo 'Target file is the following : '.$dataSaver->getfilePath().'<br />';

    echo 'Data for being saved to target file is the following : '.$dataSaver->getData();

     

    /* displays the following

    Fatal error: Call to protected method DataSaver::getfilePath() from context '' in path/to/file/

    */

    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    }


    As you can see, the PHP interpreter will trigger a fatal error each time a protected method of the "DataSaver" class is called globally. This demonstrates how useful a protected method can be when it comes to preventing it from being called outside a specific class.

    Well, I should assume that you understand how to declare and implement a couple of protected methods within a primitive PHP 5 class. Thus, based on this assumption, in the following section, I'm going to show what happens when a protected method defined in a parent class is directly invoked by a subclass. Sounds quite interesting, right?

    However, to see the complete details, you'll have to click on the link below and keep reading.

    More PHP Articles
    More By Alejandro Gervasio


       · This fourth part of the series explains how to declare and implement public and...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT