PHP
  Home arrow PHP arrow Page 2 - 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 protected methods in PHP 5-controlled environments
    ( Page 2 of 4 )

    Before I start teaching you how to define and implement private class methods in PHP 5, let me spend a brief moment refreshing some important concepts that were deployed in the last article of this series. I demonstrated how to create a trivial class that used a couple of methods, which were declared protected.

    As you'll surely recall, protected methods are preceded by the "protected" PHP keyword and can only be called from inside the originating class, or from inside the eventual subclasses. But, as always, this concept will be better grasped if you look at the following example, which shows what happens when a protected method defined within a sample class is called in the global scope.

    Here's the pertinent code sample:


    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

    protected function getFilePath(){

    return $this->filePath;

    }

    // get data via an accessor

    protected 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 protected methods

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

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

     

    /* displays the following

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

    */

    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    }


    As you can see, the above hands-on example is a no-brainer. In this particular case, I used the familiar "DataSaver" class that you learned in previous tutorials in order to demonstrate how the PHP interpreter reacts when the protected methods of the class in question are called from outside of it. It triggers a fatal error.

    Well, after examining the previous code sample, don't you feel a bit more relaxed? You should, because at this point you've been familiarized with using a couple of protected methods within a basic data saving class.

    Considering this promising scenario, it's time to jump forward and continue learning more useful things concerning the utilization of member visibility with PHP 5. Therefore, as I stated in the introduction, in the course of the following section, I'm going to teach you how to use the same sample class listed previously, but this time to declare and implement private methods.

    Are you curious about how this will be done? Don't hesitate to read the next few lines. I'll be there, waiting for you.



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