Home arrow PHP arrow Page 4 - Protecting PHP 5 Class Data with Member Visibility

Accessing protected properties using class accessors - PHP

If you ever have the chance to gather a group of PHP programmers in a big room and ask them whether they find it hard to build classes with PHP, the invariable answer would probably be a resounding "No!" And admittedly, they’d be absolutely right, since PHP allows you to work with classes, methods, and properties in a pretty straightforward way. But there's more to programming with classes, as this article series will explain.

TABLE OF CONTENTS:
  1. Protecting PHP 5 Class Data with Member Visibility
  2. Making the properties of a class available in the global scope
  3. Restricting the access to class properties
  4. Accessing protected properties using class accessors
By: Alejandro Gervasio
Rating: starstarstarstarstar / 11
May 21, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In consonance with the concepts that I expressed in the section that you just read, the last example that I’m going to provide in this tutorial is aimed at demonstrating how the protected properties of the “DataSaver” class, which was created earlier, can be accessed from inside the class in question.

How will this be done? It’s very simple really, since, as you may have noticed, this sample class has a couple of access methods (also called accessors), named “getData()” and “getFilePath()” respectively. They can be used to retrieve the values of the protected properties from inside the class.

But to clarity things a bit more, I'm going to list the complete definition of the pertinent “DataSaver” class, to remind you of how these access methods look. Here it is:


// define 'DataSaver' class (properties are defined protected but are accessed by the accessors)


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;

}

}


Now that you know how the access methods of the above “DataSaver” class have been implemented, please pay close attention to the following example, which shows how to output the values of the respective protected properties via the previously mentioned methods:


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

// print value of protected properties

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

Target file is the following : datafile.txt

Data for being saved to target file is the following : This string of data will be saved to a target file!

*/

}

catch(Exception $e){

echo $e->getMessage();

exit();

}


Now the protected properties that belong to the pertinent data saving class are accessed from inside the class scope via the respective “getData()” and “getFilePath()” methods. This demonstrates how to retrieve their corresponding values within the class in question.

At this point you’ll have to agree with me that working with protected class properties in PHP 5 is a pretty straightforward process that can be learned in a very short time. However, if you’re not feeling completely confident using this  feature that's incorporated into the object model of PHP, you may want to play with all the code samples developed in this first chapter of this series.

Final thoughts

In this initial tutorial, I walked you through the foundations of member visibility in PHP 5. In this case, you hopefully learned how to work with public and protected class properties. This is actually a no-brainer process that can be tackled with minor efforts.

In the upcoming article of the series, I’m going to teach you how to restrict the access to the properties of a class even more by declaring them private, so I don’t think you want to miss it!



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