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

Retrieving an object in a different PHP file - 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

In the previous segment, the properties assigned to an object derived from the sample ďUserĒ class were saved to a text file. Having discussed the underlying logic of this process, the only thing that remains undone is showing how those properties can be easily retrieve by a different PHP file.

Taking into account this requirement, below I defined such a file. It performs the retrieval process and assigns new values to the respective properties before storing them again in the target file. Hereís how this file looks:

<?php

 

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

}

}

 

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

 

?>

Hopefully, if all has gone well, after running the previous file, the name and email of my friend Susan have been saved to the ďdata.txtĒ file. thus preserving these properties for further handling. This example proves how simple it is to create objects that are capable of saving their properties to a predefined text file.

Of course, itís valid to point out that in this case only the objectís properties are stored, so if an application also needs to preserve the methods, then an approach that uses serialization/unserialization would probably be the best option to pick up.

Finally, feel free to introduce your own enhancements to the example class shown before, so you can sharpen your existing skills in building persistent objects in PHP 5.

Final thoughts

In this third part of the series, you learned how to build a basic PHP 5 class that could store instances of itself to a predefined text file, thus exploring yet another approach to creating persistent objects in a straightforward fashion.

In the upcoming tutorial, Iím going to enhance the existing functionality of this sample class by providing it with the capacity for using different text files as its default storage mechanism.

Hereís my final piece of advice: donít miss the next 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: