Home arrow PHP arrow Page 2 - Using Subclasses and Accessors with Member Visibility to Protect PHP 5 Class Data

Accessing private properties globally - PHP

Itís quite probable that you want to see what happens when a private property defined by a parent is retrieved from inside a child class, or if itís possible to access it via an ďaccessorĒ method. Therefore, in the next few lines, Iím going to develop a bunch of practical examples that will help you grasp how to work with private properties in these situations.

TABLE OF CONTENTS:
  1. Using Subclasses and Accessors with Member Visibility to Protect PHP 5 Class Data
  2. Accessing private properties globally
  3. Accessing private properties from a subclass
  4. Retrieving the values of private properties using accessors
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
June 04, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Before I start showing you how to access some private properties directly from a child class, first, let me reintroduce a practical example that I developed in the previous tutorial. It illustrated how the PHP interpreter behaves when it accesses a couple of private class properties globally.

As you'll possibly recall, the properties in question were declared within a rudimentary data saving class, whose signature looked like this:


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


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

public function getFilePath(){

return $this->filePath;

}

// get data via an accessor

public function getData(){

return $this->data;

}

}


Since the above "DataSaver" class was introduced in the first article of this series, I assume that you're pretty familiar with its functionality, so I won't spend any time here explaining how it works. Instead, you should pay attention to the following script, which shows what happens when the private properties of this class are accessed directly in the global scope.

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();

// try to print value of private properties

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

echo 'Data for being saved to target file is the following : '.$dataSaver->data;

 

/* displays the following

Cannot access private property DataSaver::$filePath in path/to/file/

*/

}

catch(Exception $e){

echo $e->getMessage();

exit();

}


As you can see, the prior script demonstrates how the PHP interpreter launches a fatal error each time the private properties of the previous "DataSaver" class are retrieved directly from outside of it. Even though this example is pretty basic in its definition, it helps to show how useful the member visibility feature can be when it comes to protecting the data members of a given PHP 5 class.

So far, so good, right? Now that you hopefully understand how the previous hands-on example works, it's time to learn a few more things about working with private properties of PHP 5 classes.

So, in accordance with the concepts that I went over in the beginning of this article, in the section to come, I'm going to develop yet another practical example aimed at demonstrating what happens when the private properties of the same "DataSaver" class are accessed from a subclass.

The topic will be very insightful, trust me. Thus, if you want to learn more about it, please click on the link shown below and keep reading.



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