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