WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : PHP 5 ObjectStore (Registry Pattern)
Categories : PHP, PHP Classes Click here to Update Your Picture
Joseph Crawford
Date : Aug 30th 2005
Grade : 3 of 5 (graded 2 times)
Viewed : 12531
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Joseph Crawford
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
Like this code?
Show the author your appreciation.
 

Ok so i have created a class in PHP 5 that uses The Registry Pattern, I will not explain all the details about the registry pattern. I will say that the registry pattern in short is a way to store objects. Below is the code for my ObjectStore class and an example of using it.. For sake of simplicity i am not going to include all of the code, this example builds off code in a previous example you can get that from one of my previous code examples :

http://www.weberdev.com/get_example-4221.html (Extending & Building Upon PHP5 Classes)

So what is the point of this class? Currently nothing it was done to show an example how you could use the registry pattern and store your objects accross pages without having to bulk up the session object. Is this code ready for production use? NO WAY! You should add some sort of encryption to this class. I would do a 2-way encryption algorithm where only you know the key. When you store the objects on disk you store the encrypted value. When reading you decrypt. This could also be expanded to hold objects that do not persist accross pages as well. Maybe something like $objstore->set('employee', $e, TRUE/FALSE) This would allow you to specify if the object should be serialized and saved etc..

As always Please leave comments and grades :)


Example.php
<?php
define
( 'BASE_PATH', str_replace( '\\', '/', dirname ( __FILE__ ) ) );
include_once(
'ObjectStore.php')
include_once(
'Employee.php');

try {
 
$ostore = new ObjectStore();
 
$e = $ostore->get('employee');
  if(!
is_object($e)) {
   
$e = new Employee();
   
$e->firstName('Joe');
    echo
'Object Created!';
  } else {
    echo
'Object Read From Store';
    echo
'<pre>'; print_r($e); echo '</pre>';
  }

} catch(
Exception $e) {
  echo
$e->getMessage();
}
?>



ObjectStore.php
<?php
class ObjectStore {
  private
$savepath;
  private
$store = array();

  public function
__construct() {
   
$this->savepath = BASE_PATH . '/objects/';
   
$this->loadObjects();
  }

  public function
get($key='') {
    if(
array_key_exists($key, $this->store)) {
      return
$this->store[$key];
    } else return
FALSE;
  }

  public function
set($key, $obj) {
    if(
$key != NULL && is_object($obj)) {
     
$this->store[$key] = $obj;
    }
  }

  private function
loadObjects() {
   
$d = dir($this->savepath);
    while(
false !== ($file = $d->read())) {
     
$path = $d->path.$file;
      if(
is_file($path)) {
       
$handle = fopen($path, "r");
       
$this->store[$file] = unserialize(fread($handle, filesize($path)));
       
fclose($handle);
      }
    }
  }
   
  private function
saveObjects() {
    foreach(
$this->store as $key => $obj) {
     
$handle = fopen($this->savepath.$key, "w");
     
fwrite($handle, serialize($obj));
     
fclose($handle);
    }   
  }
   
  public function
__destruct() {
   
$this->saveObjects();
  }
}
?>



A script to generate a report from a valid mysql connection. The user has to supply which fields he wants to display in table. All properties are changable.
Categories : PHP, PHP Classes, Databases, MySQL, HTML and PHP
PHP class generator, must be used from Command line interface.
Categories : PHP, PHP Classes, Shell Scripting
XPertMailer - Sends TRUE Mails
Categories : PHP, Mail, SMTP, PHP Classes
My Box - PHP Class that calculates the volumetric weight of a package.
Categories : PHP, Object Oriented, PHP Classes, Beginner Guides, Payment Gateways
ECHO-PHP Class Real Time Transaction Processor v1.4.4 for Credit Cards and Checks / ACH
Categories : PHP Classes, Cybercash, Classes and Objects, Ecommerce, PHP
ElfReader: An ELF (Executable and Linking Format) header information in PHP. Shows how to use the UNPACK function to read data.
Categories : PHP, Linux, PHP Classes
Parsing Simple Template Files and Data
Categories : PHP, PHP Classes, Templates, Regexps
Scan Apache access log files and report possible worms attack
Categories : PHP, PHP Classes, Security, Apache, Log Files
PHP CLASS for ORACLE (database connectivity)
Categories : PHP, PHP Classes, Classes and Objects, Databases, Oracle
Export Excel Dynamically to Csv then to mysql
Categories : PHP Classes, Excel, PHP
IRC Client class (RFC1459 Compliant)
Categories : PHP, PHP Classes, IRC
Simple database class
Categories : PHP, PHP Classes, MySQL, Databases
PHP interface class to the eBusiness Charts generatation remote service.
Categories : PHP, PHP Classes, Graphics, Charts and Graphs
Query2Report : Generating Html, Pdf and Csv Reports from SQL Query
Categories : PHP, PHP, HTML, PDF, Excel
A time measuring and performance benchmarking class
Categories : PHP, PHP Classes, Testing, Debugging, Date Time
 Joseph Crawford wrote :1351
You could also take this a step furthur and use php`s magic __get and __set methods ;)