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

Utilizing Private Methods with PHP 5 and Member Visibility
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      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


    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!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT