PHP
  Home arrow PHP arrow Page 2 - 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 - Making the properties of a class available in the global scope
    ( Page 2 of 4 )

    In order to explain how to work with the member visibility feature in PHP 5, I’m going to start building a basic class that will be tasked with saving an inputted string of data to a specified text file. The complete signature of this sample class is listed below, so take the time to examine it, please:


    // define 'DataSaver' class (saves a string of data to a target file and their properties are defined public)


    class DataSaver{

    public $filePath;

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

    }

    }


    The above “DataSaver” class isn’t going to change your life as a PHP developer, but it does demonstrate how to declare its two properties, called "$data” and “$filePath” as public members. This means that they can be freely accessed in the global scope.

    However, to clarify this concept, in the next few lines, I've coded a simple script that first uses the previous class to save a trivial string to a determined text file, and then prints the pertinent values of the aforementioned properties on screen.

    The corresponding code sample looks like this:


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

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

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

     

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

    }


    Since the pair of class properties have been declared public using the “public” keyword, they can be accessed directly from outside the originating class, as illustrated clearly by the previous hands-on example. In this particular case, the values assigned to these basic properties are echoed to the browser without any kind of restrictions because they’re available in the global scope. Are you starting to grasp how to declare a couple of public properties within a PHP 5 class? I guess you are!

    All right, now that you understand how to work with properties of class that have been declared public, it’s time to see how to switch their visibility to “protected.” This implies that they can’t be accessed in the global scope. They are only visible from inside the originating class and also from all their eventual subclasses.

    But actually, I’m getting ahead of myself, since this topic will be discussed in detail in the following section. Therefore, 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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek