Home arrow PHP arrow Page 3 - Defining Public and Protected Methods with Member Visibility in PHP 5

Declaring and implementing protected methods within PHP 5 classes - PHP

In the previous articles of this series, I explained how to work with public and protected class properties. So now it’s time to demonstrate how to specify these same levels of visibility in the respective methods of a class. Sounds pretty interesting, right?

TABLE OF CONTENTS:
  1. Defining Public and Protected Methods with Member Visibility in PHP 5
  2. Calling methods of a class globally
  3. Declaring and implementing protected methods within PHP 5 classes
  4. Calling protected methods from a subclass
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
June 11, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap

Dev Shed Tutorial Topics: