Home arrow PHP arrow Page 2 - Saving Class Instances to Files with Persistent Objects

Review: a basic persistent class - PHP

Welcome to the third part of a six-part series on building persistent objects in PHP 5. Through a decent variety of code samples, this series provides you with the right pointers to start creating objects that can save themselves to a persistent storage mechanism, including simple cookies, plain text files, and MySQL database tables.

TABLE OF CONTENTS:
  1. Saving Class Instances to Files with Persistent Objects
  2. Review: a basic persistent class
  3. Saving class instances to a text file
  4. Retrieving an object in a different PHP file
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
September 22, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Before I start explaining how to build a class capable of saving its instances to a text file, I'm going to reintroduce the example developed in the preceding tutorial. It demonstrated how to create a similar class, but in that particular case, it used a session variable as its default storage mechanism.

First, hereís the complete source code corresponding to that persistent class, so you can quickly recall how it originally looked:

class User

{

private $data = array();

private static $key = 'user';

 

// constructor (not implemented)

public function __construct(){}

 

// factory method

public static function factory($key = '')

{

session_start();

if ($key != '')

{

self::$key = $key;

}

if(isset($_SESSION[self::$key]) === TRUE)

{

return unserialize($_SESSION[self::$key]);

}

return new User();

}

 

// set undeclared property

public function __set($property, $value)

{

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

{

$_SESSION[self::$key] = serialize($this);

 }

}

As I explained before, the above ďUserĒ class can save an instance of itself to a custom session variable, in this case by giving a basic implementation to its destructor. On the other hand, its factory method permits you to load or create a user object, and eventually to assign new values to its properties.

These two well-differentiated processes are shown by the following script:

// example on creating a persistent object

$user = User::factory('newuser');

$user->fname = 'Alejandro';

$user->lname = 'Gervasio';

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

I believe that this code fragment should be easy for you to grasp. It first spawns a brand new user object,  and then it creates dynamically some properties along with their corresponding values. Finally, it saves the object to a session variable, which for this occasion happens to be referenced as $_SESSION['newuser'].

Once the previous user object has been stored in the session variable, say that it needs to be retrieved on a different web page for further manipulation. In a case like this, the code snippet below will perform that task in a simple manner:

// assign new values to properties of the persistent object

$user = User::factory('newuser');

$user->fname = 'Mary';

$user->lname = 'Smith';

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

Whatís more, if incidentally the previous user object is required by another file, then the whole retrieval process would be as simple as loading it via its factory method, as shown below:

$user = User::factory('newuser');

So far, so good. At this point, Iím sure that you already grasped the underlying logic that drives the previous example, meaning that itís time to explore a few other ways to create persistent objects with PHP 5. So, as I stated in the introduction, itís possible to build this type of object by using a plain text file as the default storage mechanism instead of working with cookies, which as you know can be disabled in the client.

Provided that youíre interesting in learning how to accomplish this, in the following section Iím going to define a whole new persistent class, which obviously will be capable of saving instances of itself to a specified text file.

To see how this class will be built, 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: