PHP
  Home arrow PHP arrow Page 2 - More on Private Methods with PHP 5 Mem...
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

More on Private Methods with PHP 5 Member Visibility
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2008-06-25

    Table of Contents:
  • More on Private Methods with PHP 5 Member Visibility
  • Reintroducing a previous hands-on example
  • Calling a private method from a subclass
  • Another way to protect class methods: the final keyword

  • 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


    More on Private Methods with PHP 5 Member Visibility - Reintroducing a previous hands-on example


    (Page 2 of 4 )

    As I expressed in the beginning, my plan for this final part of the series consists of completing the discussion of private methods within PHP 5 classes, as well as demonstrating how to use the “final” keyword. Thus, it’d be more than convenient to review one of the hands-on examples developed in the previous tutorial of the series. It illustrated how to declare and implement a pair of private methods within a rudimentary data saving class.

    Essentially, the example looked like this:


    // define 'DataSaver' class (methods 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

    private function getFilePath(){

    return $this->filePath;

    }

    // get data via an accessor

    private function getData(){

    return $this->data;

    }

    }


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

    // call private methods

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

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

    /* displays the following

    Fatal error: Call to private method DataSaver::getFilePath() from context '' in path/to/file/

    */

    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    }


    As you can see, a few interesting things happen: first, a basic “DataSaver” class is built, this time incorporating a couple of private methods, called “getFilePath()” and “getData()” respectively, into its API. Then, an instance of this class is created with the purpose of demonstrating how the PHP interpreter fires up a fatal error each time these methods are called from outside the originating class.

    With this simple example grasped, I will assume that you’re pretty familiar with declaring and implementing private methods within a basic PHP 5 class. Therefore, it’s time to look further into the topics that I plan to discuss in the following section.

    Well, at this point you know that when a method is declared private by a parent, it simply can’t be called by any of its eventual subclasses, right? This condition can be better understood by way of a hands-on approach, so in the next few lines I’m going to build another practical example aimed at recreating this particular situation.

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

    More PHP Articles
    More By Alejandro Gervasio


       · This last article of the series covers some additional aspects concerning the use of...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - 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
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





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