In accordance with the concepts that I expressed in the prior section, it would be pretty helpful to utilize the same data saving class that I created earlier to demonstrate how to declare and implement protected methods. Based on this simple schema, the next step will consist of listing the complete signature of the mentioned class, which now includes a couple of protected methods. That being said, here's how this sample class looks: // define 'DataSaver' class (methods are defined protected) 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; } } This time, the sample "DataSaver" class now contains two accessors, called "getData" and "getFilePath()" respectively, that have been declared protected. Essentially, this condition means that they can only be accessed within the originating class and within all of its eventual subclasses. To demonstrate the veracity of this concept, below I coded yet another basic example to help you grasp the logic that stands behind using a pair of protected methods with the previous data saving class. Here's the corresponding code sample: 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 PHP interpreter will trigger a fatal error each time a protected method of the "DataSaver" class is called globally. This demonstrates how useful a protected method can be when it comes to preventing it from being called outside a specific class. Well, I should assume that you understand how to declare and implement a couple of protected methods within a primitive PHP 5 class. Thus, based on this assumption, in the following section, I'm going to show what happens when a protected method defined in a parent class is directly invoked by a subclass. Sounds quite interesting, right? However, to see the complete details, you'll have to click on the link below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|