PHP
  Home arrow PHP arrow Page 3 - Using Subclasses and Accessors with Member Visibility to Protect PHP 5 Class Data
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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

Using Subclasses and Accessors with Member Visibility to Protect PHP 5 Class Data
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2008-06-04


    Table of Contents:
  • Using Subclasses and Accessors with Member Visibility to Protect PHP 5 Class Data
  • Accessing private properties globally
  • Accessing private properties from a subclass
  • Retrieving the values of private properties using accessors

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Using Subclasses and Accessors with Member Visibility to Protect PHP 5 Class Data - Accessing private properties from a subclass
    ( Page 3 of 4 )

    As I stated in the prior section, it'd be quite useful to demonstrate what occurs when a specific private property (defined by a parent class) is accessed by one of its subclasses. In recreating this scenario, you'll understand how helpful it is to use private data members within a particular class, especially when it comes to protecting them from unwanted access.

    This being said, here are the things that I plan to do in the next few lines: first, I'm going to list the same sample "DataSaver" class that you saw earlier. Then, I'm going to create a trivial subclass from it. And in the end, an instance of this child class will attempt to access the pair of private properties declared by the retaining parent.

    Here's the signature of the aforementioned "DataSaver" class:


    // define 'DataSaver' class (properties are defined private)


    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

    public function getFilePath(){

    return $this->filePath;

    }

    // get data via an accessor

    public function getData(){

    return $this->data;

    }

    }


    Having listed this sample data saving class, it's time to derive a subclass from it. In this case, I've called it "DataHandler," and its corresponding definition is as follows:


    // extends 'DataSaver' class and try to access private properties of the parent


    class DataHandler extends DataSaver{

    // fetch data from target file

    public function fetch(){

    if(!$data=file_get_contents($this->filePath)){

    throw new Exception('Error reading data from target file.');

    }

    return $data;

    }

    }


    That was pretty easy to grasp, wasn't it? As you can see, the above "DataHandler" subclass slightly extends the functionality of its respective parent via an additional "fetch()" method. However, the most interesting thing happens when this child class is used to access the private properties declared by the  parent.

    The following code snippet demonstrates this particular situation:


    try{

    // create new instance of 'DataHandler' class

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

    // save data to target file

    $dataHandler->save();

    // fetch data from target file

    echo $dataHandler->fetch();

    // print value of private properties

    echo 'Target file is the following : '.$dataHandler->filePath.'<br />';

    echo 'Data for being saved to target file is the following : '.$dataHandler->data;

     

    /* displays the following

    Undefined property: DataHandler::$filePath in path/to/file/

    */

    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    }


    I told you that things would be really interesting! As you can see, the above "DataHandler" class can't retrieve the value of the private properties defined by its parent, and it simply assumes that these are not defined. This example demonstrates the high level of protection that any class property will have when it's declared private.

    It's clear from the previous example that the private properties of a given class can only be accessed from inside that class in particular, and nothing else. Period.

    However, it'd be pretty useful to see if it's possible to use the accessor methods of the previous "DataHandler" class to retrieve the respective values of its private properties. What do you say? Is it possible to achieve this or not?

    To answer that intriguing question, you'll have to read the final section of this tutorial, so jump ahead and read the next few lines. I'll be there, waiting for you.



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT