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

Restoring the persistent object on a different web page - 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

As I said in the earlier segment, Iíd like to finish this tutorial by showing you how to restore the properties of the user object created previously, and how to assign new values to them as well.

The PHP file responsible for performing the restoration process would look like this:

<?php

 

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

{

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

}

}

 

// assign new properties to the persistent object

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

$user->name = 'John';

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

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

?>

Apart from including the definition corresponding to the ďUserĒ class, only three lines of code are required to retrieve the properties of its derived $user object. For example purposes, I decided to populate the properties with new values, but as you may have guessed itís possible keep the original values and use them afterward for further manipulation.

With this last example Iím finishing this article. As usual, feel free to edit all of the code samples shown in this tutorial to arm yourself with a more solid background in creating persistent objects with PHP 5.

Final thoughts

Thatís it for the moment. In this fourth chapter of the series, I discussed how to define a persistent class in PHP 5, which could save its properties to a specified text file passed previously as an input argument to the constructor. Indeed, the process was very straightforward, so in theory you shouldnít have major problems creating a class like this on your own.

Moving forward, itís time to talk about the topics that I plan to cover in the next article. Considering that MySQL is the most popular RDBMS associated with PHP 5, in that tutorial Iím going to explain how to create persistent objects that can store their properties in a MySQL database table.

My last piece of advice is simple: donít miss the upcoming part!



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