PHP
  Home arrow PHP arrow Page 2 - Defining Public and Protected Methods with Member Visibility in PHP 5
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

Defining Public and Protected Methods with Member Visibility in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2008-06-11


    Table of Contents:
  • Defining Public and Protected Methods with Member Visibility in PHP 5
  • Calling methods of a class globally
  • Declaring and implementing protected methods within PHP 5 classes
  • Calling protected methods from a subclass

  • 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


    Defining Public and Protected Methods with Member Visibility in PHP 5 - Calling methods of a class globally
    ( Page 2 of 4 )

    Working with public class methods is an extremely simple process that can be grasped in a few minutes, even by inexperienced developers. Naturally, if you've used classes in PHP 4, then you should know that all the methods you define are public by default, since there's no way to prevent them from being accessed globally.

    However, as you may have noticed, it's possible to use the "public" keyword to specify the visibility of a method in PHP 5. What's more, if no visibility is assigned to a method, then it will be declared public also by default.

    Let me stop discussing theory for a moment and show you a practical example, where you'll learn how to declare and implement a few public methods. To do this, I'm going to use the familiar "DataSaver" class that you saw in the previous articles of the series. The signature is listed below:


    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

    public function getFilePath(){

    return $this->filePath;

    }

    // get data via an accessor

    public function getData(){

    return $this->data;

    }

    }


    As you can see, all of the methods incorporated into the above class are declared public, which means that they can be called directly from outside the class in question. To understand this concept, look at the following script, which shows how to invoke some methods of the sample "DataSaver" class in the global scope:


    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 public 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

    Target file is the following : datafile.txt

    Data for being saved to target file is the following : This string of data will be saved to a target file!

    */

    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    }


    So far, there's nothing unexpected. As you can see, because the "save()," "getFilePath()," and "getData()" methods of the above sample class have been declared public, they can be invoked directly from outside the mentioned class and the PHP interpreter won't trigger any fatal errors.

    Using public methods with PHP 5 is a no-brainer process, but even so, you should be very careful when declaring the visibility of methods that make up your own classes, particularly if you don't want them to be accessed in the global context.

    Well, assuming that you grasped how to work with public class methods, it's time to move forward and continue with this educational journey. Therefore, in the course of the following section, I'm going to utilize the same data saving class that you saw before in order to explain how to declare and implement protected methods.

    To learn the complete details on this useful topic, please go ahead and read the next few lines. They're only one click away.



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