PHP
  Home arrow PHP arrow Page 3 - Working with Private Properties to Pro...
Dev Shed Forums 
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

Working with Private Properties to Protect PHP 5 Class Data
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2008-05-28

    Table of Contents:
  • Working with Private Properties to Protect PHP 5 Class Data
  • A review of public and protected class properties in PHP 5
  • Extending the use of protected class properties: working with a subclass
  • Defining private class properties

  • 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


    Working with Private Properties to Protect PHP 5 Class Data - Extending the use of protected class properties: working with a subclass


    (Page 3 of 4 )

    As I explained in the prior section, the properties of a class that have been declared protected can be accessed freely by any subclass derived from the corresponding parent. In order to demonstrate this concept, below I coded a basic example that shows how the protected properties of the previous “DataSaver” class can be retrieved by a subclass.

    Here’s the code sample; examine it in detail, please:


    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;

    }

    }


    // extends 'DataSaver' class and try to access protected properties


    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;

    }

    }


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

     

    /* displays the following

    This string of data will be saved to a target file!

    */

    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    }


    Now things are getting really interesting! As you can see, first, I derived a simple subclass from the pertinent “DataSaver” parent, and then used the brand new “fetch()” method to access the protected “$filePath” property. In doing so, I’m demonstrating how a protected property can be retrieved from inside a child class. Quite simple to understand, right?

    All right, at this stage you've hopefully learned how to get access to a pair of protected properties defined by a parent using a child class. So the question is: what comes next?

    Well, as I said in the beginning of this article, PHP 5 supports the definition of private properties via the “private” keyword. Thus, in the section to come, I’m going to teach you how to work with properties that have that specific level of visibility.

    Naturally, in order to learn more about this useful topic, you’ll have to click on the link below and keep reading.

    More PHP Articles
    More By Alejandro Gervasio


       · This chapter of the series covers the declaration and use of private class...
     

       

    PHP ARTICLES

    - Using Aliases and the Autoload Function with...
    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - 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
    - Sub Classing Exceptions in PHP 5
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




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