PHP
  Home arrow PHP arrow Page 3 - Protecting PHP 5 Class Data with Member Visibility
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

Protecting PHP 5 Class Data with Member Visibility
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 10
    2008-05-21


    Table of Contents:
  • Protecting PHP 5 Class Data with Member Visibility
  • Making the properties of a class available in the global scope
  • Restricting the access to class properties
  • Accessing protected properties using class 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


    Protecting PHP 5 Class Data with Member Visibility - Restricting the access to class properties
    ( Page 3 of 4 )

    Throughout the course of the previous section, I showed you how to declare a couple of public properties within a sample class in order to make them globally accessible. Assuming that you grasped how to achieve this, the next thing that I’m going to teach you will be how to build the same class, but this time I will define the respective properties as protected data members.

    The signature of the pertinent “DataSaver” class will look like this:


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


    class DataSaver{

    protected $filePath;

    protected $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;

    }

    }


    Obviously, the definition of the above “DataSaver” class is nearly identical to the one that you learned in the previous section, except now the respective “$data” and “$filePath” properties have been declared protected by preceding them with the “protected” keyword. But what does this mean in simple terms?

    Well, by definition, any data member defined as “protected” within a PHP 5 class can only be accessed from inside the originating class, and from inside all the eventual subclasses. This implies that they’re no longer globally available.

    Of course, this concept will be better understood by example. So, below I set up another code sample that demonstrates how the PHP interpreter triggers a fatal error when the protected class properties are wrongfully accessed in the global scope:


    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 protected properties

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

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

     

    /* displays the following fatal error with the first property

    Fatal error: Cannot access protected property DataSaver::$filePath in path/to/file/

    */

    /* displays the following fatal error with the second property

    Fatal error: Cannot access protected property DataSaver::$data in path/to/file/

    */

    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    }


    Pretty simple to grasp, right? As you can see in the above example, the PHP 5 engine will fire up a fatal error each time a protected property is accessed from outside the originating class. This implements an effective restriction mechanism that can be applied to both properties and methods of a given class.

    However, as I explained before, the properties in question can be accessed without any restrictions from inside the originating class. Therefore, in the last section of this tutorial I’m going to show you yet another practical example where this condition will be recreated for you.

    To see how this final example will be developed, please click on the link below and keep reading.



     
     
    >>> 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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek