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

A review of public and protected class properties in PHP 5 - 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

In the introduction, I provided you with a quick reference regarding the use of public and protected class properties in PHP 5. However, I’d like to complement this theory with the hands-on examples developed in the preceding article of the series. This way, you can tackle this topic from a practical point of view.

Having said that, please take a look at the following code samples, which demonstrate how to use both public and protected properties using the same sample class. Here they are:


(example using public properties with a data saving class)


// define 'DataSaver' class (saves a string of data to a target file and their properties are defined public)


class DataSaver{

public $filePath;

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

}

}

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

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

}



(example using protected properties with a data saving class -- properties are defined protected)


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;

}

}


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->filePath.'<br />';

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

 

/* displays the following fatal error with the first property

Fatal error: Cannot access protected property DataSaver::$filePath in path/to/file/

*/

 

/* displays the following fatal error with the second property

Fatal error: Cannot access protected property DataSaver::$data in path/to/file/

*/

}

catch(Exception $e){

echo $e->getMessage();

exit();

}


At this point, you should feel pretty satisfied, since the hands-on examples listed above give you a clear idea of how to work with public and protected properties using the same sample “DataSaver” class. In the first case, you can see that the properties in question are accessible in the global scope, since they’ve been declared public. In the second example, the PHP interpreter triggers a few fatal errors when trying to access the properties publicly, because the mentioned properties have been defined as protected. Not too hard to grasp, is it?

So far, so good. Now that you've hopefully learned the basic concepts that surround the use of public and protected properties with PHP 5 classes, it’s time to explore more useful aspects of the utilization of member visibility.

Even when protected properties can’t be accessed directly in the global scope, they’re not restricted when used by one or more subclasses derived from the pertinent parent. Therefore, in the following section, I’m going to create another practical example for you that will recreate the scenario described above.

In order to see how this brand new example will be developed, go ahead and read the next few lines. They’re only one click away.



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