PHP
  Home arrow PHP arrow Page 4 - Utilizing Private Methods with PHP 5 a...
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

Utilizing Private Methods with PHP 5 and Member Visibility
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2008-06-18

    Table of Contents:
  • Utilizing Private Methods with PHP 5 and Member Visibility
  • Working with protected methods in PHP 5-controlled environments
  • Working with private data members
  • Accessing private methods with a public one

  • 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


    Utilizing Private Methods with PHP 5 and Member Visibility - Accessing private methods with a public one


    (Page 4 of 4 )

    As I said in the course of the previous section, the last thing that I'm going to teach you with reference to using private methods in PHP 5 will consist of demonstrating how these can be accessed from outside the originating class by way of a public method.

    Given that, first I'm going to modify the signature of the sample "DataSaver" class that you learned earlier, and code a brand new method, called "getAllData()," which will come in handy for accessing the pair of private methods defined previously.

    Based on this idea, here's the modified definition of the pertinent "DataSaver" 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

    private function getFilePath(){

    return $this->filePath;

    }

    // get data via an accessor

    private function getData(){

    return $this->data;

    }

    // get all data

    public function getAllData(){

    return 'Target File : '.$this->getFilePath().' Data : '.$this->getData();

    }

    }


    As you can see, the above "DataSaver" class now implements an additional public "getAllData()" method, which is responsible for calling the private "getFilePath()" and "getData()" methods respectively. Based on the implementation of the mentioned "getAllData()," it's pretty easy to demonstrate how a couple of private methods can be accessed by a public one.

    The below script recreates this specific situation for you. Here it is:


    try{

    // create new instance of 'DataHandler' 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 via a public one

    echo $dataSaver->getAllData();

     

    /* displays the following

    Target File : datafile.txt Data : This string of data will be saved to a target file!

    */

    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    }


    That was quite easy to grasp, wasn't it? As you can see, by way of a simple workaround, like the above "getAllData()" public method, it's perfectly possible to call the private "getFilePath()" and "getData()" methods respectively, without getting any fatal errors from the PHP interpreter.

    As usual with many of my articles on PHP development, I suggest you practice using the code samples developed in this tutorial, so you can get more experience in working with private methods in PHP 5.

    Final thoughts

    In this fifth installment of the series, I provided you with a quick overview of how to declare and implement private methods within a PHP 5 class. As you learned before, this process is actually quite simple to tackle and only requires preceding the method that you want to declare private with the "private" keyword. That's all you need!

    In the final article of the series, I'll be teaching you a few additional things concerning the use of private methods in PHP 5, such as creating an additional example that will help you understand what happens when a private method is called from within a subclass. In addition, I'll be showing you how to utilize the "final" PHP 5 keyword, which can be helpful for protecting the data members of your classes from undesired access.

    Now that you've been warned of the topics that will be discussed in the last article of this series, you won't want to miss it!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · This fifth episode of this series discusses with some simple code samples, how to...
       · Alejandro, Your articles are really great and very useful. Best Regards !
       · Thank you for the kind comments on my PHP articles, and it’s always good to know...
     

       

    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 5 hosted by Hostway
    Stay green...Green IT