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

Review: saving object properties to a predetermined text file - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

If you still haven’t read the previous installment of this series, where I explained how to build a basic class that could save its properties to a predefined text file, don't worry. Below I've included this class’s complete definition, along with a short script that demonstrates how to use it.

Here’s the full source code of this persistent class, so pay attention to it:

class User

{

private $data = array();

private $file = 'data.txt';

 

// constructor

public function __construct()

{

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')

{

return;

}

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

 }

}

Definitely, the above code fragment demonstrates how simple it is to build a class that can save the properties of its instances to a specific file, in this particular case called “data.txt.” Of course, the implementation of its destructor makes the whole saving process really simple. It's performed transparently when the PHP interpreter invokes this method before it finishes executing the calling script.

And now that you grasped the logic driving the previous class, here’s a script that populates the properties of a user object with some trivial values, and then stores them in the specified text file:

// example on using a persistent object with a text file

$user = new User();

$user->name = 'Mary';

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

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

Once the properties have been properly saved to the “data.txt” file, it’s possible to retrieve them on a different web page, as shown below, if the definition of the class has been previously included. Take a look:

// assign new values to properties of the persistent object

$user = new User();

$user->name = 'Susan';

$user->email = 'susan@domain.com';

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

There you have it. As you can see, creating persistent objects in PHP 5 is much easier than you might think, right? However, in keeping with the concepts described in the introduction, it’s feasible to make the example “User” class a bit more flexible by providing it with the ability to save its instances to a text file other than the default “data.txt.”

But how can this be done in a few easy steps? Well, the upgrade process would be as simple as refactoring the class’s constructor. The constructor needs to be able to take, as an input argument, the name of the file that will be used for storing the corresponding properties.

The full details of this process will be discussed in detail in the section to come. To learn more, read the following segment.



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