PHP
  Home arrow PHP arrow Page 3 - 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? 
Google.com  
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 - Working with private data members
    ( Page 3 of 4 )

    If you found it easy to learn how to declare and implement protected methods using the "DataSaver" class, then I'm pretty sure that there will be a big smile on your face when I teach you how to work with private ones!

    Well, I possibly got caught in the moment, but trust me, defining private methods in PHP 5 is a really simple process that only requires preceding the method that you wish to declare private with the "private" keyword. It's simple and intuitive, isn't it?

    Besides, it's important to say that any method defined as private can only be called from inside the originating class. Period. In this case, it can't be accessed by any subclass, which restricts a method even more from unwanted calls.

    Even so, the previous explanation would be rather incomplete if I didn't show you an example of how useful a private method can be in concrete situations. So, once more I'm going to appeal to the sample "DataSaver" class to show you how to declare and implement a pair of private methods.

    This being said, take a look at the code sample below, which includes the modified signature of this sample class and shows how the PHP engine throws a fatal error when one of these methods is called in the global scope:


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

    }


    Didn't I tell you that using private methods with the above data saving class was going to be a straightforward process? And certainly, I was correct, since the previous example shows you very clearly how the PHP interpreter fires up a fatal error when a private method is directly called from outside the class in question.

    With the previous example at your disposal, you can hopefully learn the basics of defining and implementing private methods within a basic PHP 5 class. So, what's the next step to take from here? Well, it's probable that you're wondering how a private method can be called in the global scope without getting a fatal error.

    In practical terms, this isn't possible. However, there's something that can be done to skip over this restriction, and it consists of simply using a public method to access a private one. Indeed, this idea may sound confusing, but it can be used to expand your recently-acquired background in working with private methods.

    Thus, in the section to come, I'm going to set up a final example for you that will recreate this particular situation.

    Please, 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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek