Home arrow PHP arrow Page 3 - Working with Private Properties to Protect PHP 5 Class Data

Extending the use of protected class properties: working with a subclass - PHP

Welcome to the second installment of the series, “Protecting the data of PHP 5 classes with member visibility.” It is made up of six comprehensive tutorials and provides you with a guide to help you get started using member visibility with PHP 5. It also complements the corresponding theoretical concepts with illustrative hands-on examples.

TABLE OF CONTENTS:
  1. Working with Private Properties to Protect PHP 5 Class Data
  2. A review of public and protected class properties in PHP 5
  3. Extending the use of protected class properties: working with a subclass
  4. Defining private class properties
By: Alejandro Gervasio
Rating: starstarstarstarstar / 5
May 28, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I explained in the prior section, the properties of a class that have been declared protected can be accessed freely by any subclass derived from the corresponding parent. In order to demonstrate this concept, below I coded a basic example that shows how the protected properties of the previous “DataSaver” class can be retrieved by a subclass.

Here’s the code sample; examine it in detail, please:


class DataSaver{

protected $filePath;

protected $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;

}

}


// extends 'DataSaver' class and try to access protected properties


class DataHandler extends DataSaver{

// fetch data from target file

public function fetch(){

if(!$data=file_get_contents($this->filePath)){

throw new Exception('Error reading data from target file.');

}

return $data;

}

}


try{

// create new instance of 'DataHandler' class

$dataHandler=new DataHandler('This string of data will be saved to a target file!','datafile.txt');

// save data to target file

$dataHandler->save();

// fetch data from target file

echo $dataHandler->fetch();

 

/* displays the following

This string of data will be saved to a target file!

*/

}

catch(Exception $e){

echo $e->getMessage();

exit();

}


Now things are getting really interesting! As you can see, first, I derived a simple subclass from the pertinent “DataSaver” parent, and then used the brand new “fetch()” method to access the protected “$filePath” property. In doing so, I’m demonstrating how a protected property can be retrieved from inside a child class. Quite simple to understand, right?

All right, at this stage you've hopefully learned how to get access to a pair of protected properties defined by a parent using a child class. So the question is: what comes next?

Well, as I said in the beginning of this article, PHP 5 supports the definition of private properties via the “private” keyword. Thus, in the section to come, I’m going to teach you how to work with properties that have that specific level of visibility.

Naturally, in order to learn more about this useful topic, you’ll have to 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
   

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: