Home arrow PHP arrow Page 3 - Using Different Target Files for Persistent Objects

Saving class properties to different text files - PHP

Persistent objects appear at first to be an obscure and hard-to-grasp subject that belongs under software development. In fact, persistent objects are simply regular objects spawned from a class that has some form of storage mechanism associated with it. This six-part series shows you how to get the most out of persistent objects in your web applications.

  1. Using Different Target Files for Persistent Objects
  2. Review: saving object properties to a predetermined text file
  3. Saving class properties to different text files
  4. Restoring the persistent object on a different web page
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
September 28, 2009

print this article



As I said in the previous section, it’d be desirable to extend the functionality of the example “User” class by making it able to save its properties to a different text file. This can be achieved in several ways, but in this case I’m simply going to redefine the class’s constructor, as shown below:

class User


private $data = array();

private $file = 'data.txt';


// constructor

public function __construct($file = '')


if ($file != '')


$this->file = $file;


list($this->data['name'], $this->data['email']) = explode('|', file_get_contents($this->file));



// set undeclared property

public function __set($property, $value)


if ($property !== 'name' and $property !== 'email')




$this->data[$property] = $value;



// get undeclared property

public function __get($property)


if (isset($this->data[$property]) === TRUE)


return $this->data[$property];




// save object to session variable

public function __destruct()


file_put_contents($this->file, $this->name . '|' . $this->email);



As you can see above, now the constructor of the “User” class has been slightly modified to accept as an incoming argument the name of the file that will be used to store the corresponding properties. This subtle change definitely permits much more more flexibility when creating a persistent object.

To understand this concept more clearly, I suggest that you look at the following code sample. It will first spawn a new user object, and then assign some basic properties dynamically; finally, it will save those properties to a different text file, this time called “newfile.txt.” Here’s the example:

// example on creating a persistent object

$user = new User('newfile.txt');

$user->name = 'Alejandro';

$user->email = 'alejandro@mydomain.com';

// __destruct() saves automatically the object to the target file

Undeniably, the previous code fragment is very easy to follow, so I won’t bore you by explaining once again how it works. It's valid to note here, though, how a small change introduced into the originating class of this persistent object can have a considerable impact on its functionality. Pretty good, right?

At this point, naturally the properties of the previous user object have been neatly saved to the target “newfile.txt” file, but this is only the half of the story. It's necessary to show how those properties can be retrieved in a different file, thus proving that they’re capable of persisting across different HTTP requests.

With that goal in mind, in the last section of this tutorial I’m going to code another short script, which will illustrate the full details of this retrieval process.

Now, click on the link that appears below and read the final segment. We’re almost finished!

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