Home arrow PHP arrow Page 4 - PHP Datastorage Class (continued)

A Snack for the Road - PHP

In the first part of this two-part article, you started to learn about using alternatives to databases for storing data; specifically, we started to work on creating a class that can handle flat files, session variables, and cookies. This second part picks up right where we left off last time.

TABLE OF CONTENTS:
  1. PHP Datastorage Class (continued)
  2. Utility Methods
  3. The Output Methods in Depth
  4. A Snack for the Road
By: Chris Root
Rating: starstarstarstarstar / 5
November 21, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Finally, if you are going to use cookies to store data, the constructor method should have the following code in it at the last If block.

else if($mode == "c")
{
      if(isset($_COOKIE[$this-dataname]))
      {
            $this->items = unserialize($_COOKIE[$this->dataname]);
      }
      else
      {
            setcookie($this->dataname);
      }
}

If the cookie is not yet set, then it is set, otherwise we retrieve the data from it just as we would with any of the other storage methods. This code simply sets a cookie by the default or the name specified. If you wish to supply additional parameters, such as expiration time, these optional arguments would need to be added to the constructor. You might also want to provide a backup plan in case the user has cookies disabled.

Wrapping it Up

Because all of this functionality is wrapped in a class, it can be included and extended by other classes easily.You can then let the err method do the work. You can also have multiple instances of the datastore in a document. Adding database connection capability through an abstraction class would give you a complete data storage solution, and the datastore could be used to provide preset connections and queries. If you come up with a good use make sure to share it.



 
 
>>> More PHP Articles          >>> More By Chris Root
 

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap

Dev Shed Tutorial Topics: