PHP
  Home arrow PHP arrow Page 2 - 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 - 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


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