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!
blog comments powered by Disqus |
|
|
|
|
|
|
|