Home arrow PHP arrow Page 2 - More on Private Methods with PHP 5 Member Visibility

Reintroducing a previous hands-on example - PHP

Welcome to the final chapter of the series, “Protecting the data of PHP 5 classes with member visibility.” Complemented by copious illustrative examples, this article series guides you through learning the key concepts regarding the use of public, protected, and private data members within PHP 5 classes. This way you can start defining the visibility of the properties and methods of your own classes in a very short time.

  1. More on Private Methods with PHP 5 Member Visibility
  2. Reintroducing a previous hands-on example
  3. Calling a private method from a subclass
  4. Another way to protect class methods: the final keyword
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
June 25, 2008

print this article



As I expressed in the beginning, my plan for this final part of the series consists of completing the discussion of private methods within PHP 5 classes, as well as demonstrating how to use the “final” keyword. Thus, it’d be more than convenient to review one of the hands-on examples developed in the previous tutorial of the series. It illustrated how to declare and implement a pair of private methods within a rudimentary data saving class.

Essentially, the example looked like this:

// define 'DataSaver' class (methods are defined private)

class DataSaver{

private $filePath;

private $data;

public function __construct($data,$filePath){


throw new Exception('Invalid data for being saved to target file.');



throw new Exception('Invalid target file.');





// save data to target file

public function save(){


throw new Exception('Error opening target file.');



throw new Exception('Error writing data to target file.');




// get target file via an accessor

private function getFilePath(){

return $this->filePath;


// get data via an accessor

private function getData(){

return $this->data;




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


// call private methods

echo 'Target file is the following : '.$dataSaver->getFilePath().'<br />';

echo 'Data for being saved to target files is the following : '.$dataSaver->getData();

/* displays the following

Fatal error: Call to private method DataSaver::getFilePath() from context '' in path/to/file/



catch(Exception $e){

echo $e->getMessage();



As you can see, a few interesting things happen: first, a basic “DataSaver” class is built, this time incorporating a couple of private methods, called “getFilePath()” and “getData()” respectively, into its API. Then, an instance of this class is created with the purpose of demonstrating how the PHP interpreter fires up a fatal error each time these methods are called from outside the originating class.

With this simple example grasped, I will assume that you’re pretty familiar with declaring and implementing private methods within a basic PHP 5 class. Therefore, it’s time to look further into the topics that I plan to discuss in the following section.

Well, at this point you know that when a method is declared private by a parent, it simply can’t be called by any of its eventual subclasses, right? This condition can be better understood by way of a hands-on approach, so in the next few lines I’m going to build another practical example aimed at recreating this particular situation.

To see how this brand new example will be developed, click on the link below and keep reading.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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