Home arrow PHP arrow Page 4 - Utilizing Private Methods with PHP 5 and Member Visibility

Accessing private methods with a public one - PHP

Welcome to the fifth chapter of the series, “Protecting the data of PHP 5 classes with member visibility.” Comprised of six parts, this series teaches you how to define the levels of visibility that properties and methods of a specific class will have when developing PHP5-controlled applications. This article will show you how to declare methods and properties private.

TABLE OF CONTENTS:
  1. Utilizing Private Methods with PHP 5 and Member Visibility
  2. Working with protected methods in PHP 5-controlled environments
  3. Working with private data members
  4. Accessing private methods with a public one
By: Alejandro Gervasio
Rating: starstarstarstarstar / 5
June 18, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: