PHP
  Home arrow PHP arrow Page 4 - 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? 
Google.com  
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 - Retrieving the values of private properties using accessors
    ( Page 4 of 4 )

    During the previous section, you learned a simple -- yet crucial -- concept: the private properties of a class can only be accessed from inside of the class. However, it's possible to play around with this theoretical definition and develop yet another example that shows how the properties in question can be retrieved by way of an accessor method.

    To perform this task, I'm going to use the same "DataSaver" class that you saw previously, which, as you'll surely recall, does have a couple of these accessor methods. If this doesn't ring any bells, here's the signature of this class:


    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;

    }

    }


    As you can see, the above data saving class has been provided with a couple of accessors, called "getFilePath()" and "getData()" respectively. With these methods available, I'm going to create a simple script that shows how to use them in order to access the private properties of the class.

    Having said that, here's the script:


    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();

    // print value of private properties

    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

    Target file is the following : datafile.txt

    Data for being saved to target file is the following : This string of data will be saved to a target file!

    */

    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    }


    As shown by the previous code snippet, it's perfectly possible to retrieve the values that correspond to the private properties of the "DataSaver" class by using the respective accessors. The reason is extremely simple: the methods access the properties from inside the class!

    Of course, this concept may seem pretty obvious, particularly if you have a lot of experience working with classes in PHP. But if you're just getting started using its object model, the previous example might help you understand how to work with private properties.

    Finally, with all of these code samples at your disposal, don't excuse yourself from practicing with private class properties in PHP 5!

    Final thoughts

    In this third episode of the series I concluded this quick overview of working with public, protected, and private class properties. Nonetheless, I'm only scratching the surface when it comes to exploring the advantages of using member visibility in PHP 5; the language permits you to use this same feature with the methods of a class as well. Thus, in the upcoming article, I'll explain how to implement this handy feature in several class methods.

    Now that you've been warned about the subject of the next part, you can't miss it!



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

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek