Heya everyone,

Introduction

What you will learning to day is how to create a registry type of system for your "Framwork/Website" and how it will help you to manage all of your objects.

What is a Registry you say?
A registry is a class that will be used to hold other classes in an array/stdClass and create a type of "SperGlobal" for your script.

The Steps.
  1. 1. Create index.php
  2. 2. Create create a bootloader
  3. 3. Create the registry
  4. 4. Create create some sub-classes
  5. 5. Use sub-class via registry


Step 1. (Create index.php)

The index.php is extreamy simple to create as this is the just an output file so to speak.... its the very first file that initiates the core logic and is what files you will be creating for new pages on your site.. this is why it has to be simple.

PHP Code: 
<?php

/*
**
*** First lets create a constant that we will be using for 2 main things.
*** 1. Used for security
*** 2. Used to help know where we am on the filesystem.
*** E.G. of the value is /root/sites/mySite/htdocs/
**
*/

define("APPLICATION_PATH" str_replace("\\","/",dirname(__FILE__)) . "/");

/*
**
*** No lets include the bootloader file
**
*/
require_once APPLICATION_PATH "system/boot.php";
?>
Step 2. (Create the bootloader)

What the bootloader will be doing in this "system" is
  1. 1. including all the main class files[/*]
  2. 2. Creating an instance of the registry object[/*]
  3. 3. Creating an instance of other objects and adding to the registry.[/*]


Create a new directory next to "index.php" and call it system.. after that just create a new php document and call it "boot.php"

PHP Code: 
<?php

/*
**
*** In step one we created a constant called APPLICATION_PATH.
*** Now a smarty security check is to make sure that constant is set.
*** This will stope hackers from directly accessing the file from the browser.
*** So we just make sure that the APPLICATION_PATH is in the scope
**
*/

if(!defined("APPLICATION_PATH")){ die("No DIRECT Access"); }

/*
**
*** Now at this point we can start including our files, and creating the objects etc.
*** The first object were going to be including is the regsitry object.
**
*/

require_once APPLICATION_PATH "system/includes/classes/registry.php";

/*
**
*** As the object will be a "static" class we wont need to create an instance of the object
**
*/
?>
At a later date in the script we will add more includes etc to the bootloader but nothing for now
[*]3. (Create the registry)[/*]
Firstly we nned to create a 2 new directories and a new php file so

1st. create a directory in "system" called "includes" and then within includes called "classes" so we have this structure "system/includes/classes/"

2nd. Create a new php file called Registry.php with capital R for consistancy.

lets take a look at the Regsitry class now
PHP Code: 
<?php

/*
**
*** The registry class contains 2 "Magic Methods" and one object holder
*** Tip: if your not sure about magic methods, please look at Stephans videos on PHP6
*** http://killerphp.com/tutorials/advanced-php/
*** Lets create the object now
**
*/

class Registry{
    
    
/*
    **
    *** The $objects variable will contain all the classes/variables/data
    **
    */
    
var $objects = array();

    
/*
    **
    *** The __constructor method will run when the class is first created
    *** Please not that in the constructor it should take 0 args if posible
    **
    */
    
public function __construct(){
    }
    
    
/*
    **
    *** The __set magic method will be used to add new objects to the $objects
    *** (http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members)
    **
    */
    
    
public function __set($index,$value){
        
$this->objects[$index] = $value;
    }
    
    
/*
    **
    *** The magic method __get will be used when were trying to pull objects from the storage variable
    **
    */
    
    
public function __get($index){
        return 
$this->objects[$index];
    }
    
    
/*
    **
    *** The magic methos sleep and wake are used to compress the data when there not being used, this helps save system
    *** Resources if your Registry gets on the larger side.
    **
    */
    
    
function __sleep(){ /*serialize on sleep*/
        
$this->objects serialize($this->objects);
    }
    function 
__wake(){ /*un serialize on wake*/
        
$this->$objects unserialize($this->objects);
    }
}
?>
Ok so the basic outlay of the Registry is like a big box... and this box floats in mid air "so to speak" the reason its a box is because you can put any thing inside it.. I.e Data/Objects/Arrays etc.. and the reason i used the metaphore "Floats in mid air" is due to the fact its a static class and you will be able to used it everywhere in the script!

step 4. (Create create some sub-classes)

Ok now first what were going to be doing is creating a libary folder so we can store a web related classes there.

so if you like to go a head and create a directory called "libary" within the includes folder

our directory structure should be like "system/includes/libary/"...

After you have that folder we can now add our first class witch will be an Http input class
(used for working with the global $_GET,$_POST,$_COOKIE).

Go ahead and create a file called HTTPInput.class.php and place within the "libary" folder

Below is just an small example of a HTTP Input. if you want to look at one more in-depth the download the CodeIgnighter source package and look in there libary.

PHP Code: 
<?php
if(!defined("APPLICATION_PATH")){ die("No DIRECT Access"); }

final class 
HttpInput{
    
    function 
__construct(){
        
$this->unregistryGlobals(); // sample function you ahve to build.
        //Clean user input
        
$_GET        $this->_processGET($_GET);
        
$_POST        $this->_processPOST($_POST);
        
$_COOKIE    $this->_processCOOKIE($_COOKIE);
        
$_FILES        $this->_processCOOKIE($_FILES);
    }
    
    
//Cleaning Functiions
    
private function _processGET($_){
        
//Here we will just be recursivly escaping the data for example usage
        
if(is_array($_)){
            
$tmp = array();
            foreach(
$_ as $key => $val){
                
//Check the $key is valid here if you wish
                
$tmp[$key] = $this->_processGET($val);
            }
            return 
$tmp;
        }
        
//Here we can check the single values
        
if(get_magic_quotes_gpc()){
            
$_ stripslashes($_);
        }
    
//Other checking here....
    
    //Return
    
return $_;
    }
    private function 
_processPOST($_){
        
//Check Post
    
}
    private function 
_processCOOKIE($_){
        
//Check Cookie    
    
}
    private function 
_processFILES($_){
        
//Check Files
    
}    
    private function 
unregistryGlobals(){
        
//Here you can do some work on unregistering globals for security etc
    
}
}
?>
No we have created the HTTP-Input class we have to tell the boot.php to use it and we need to place it within the registry.

so withing your boot.php we will be adding some more code at the bottom.. so it should now look like the following.

PHP Code: 
<?php

/*
**
*** In step one we created a constant called APPLICATION_PATH.
*** Now a smarty security check is to make sure that constant is set.
*** This will stope hackers from directly accessing the file from the browser.
*** So we just make sure that the APPLICATION_PATH is in the scope
**
*/

if(!defined("APPLICATION_PATH")){ die("No DIRECT Access"); }

/*
**
*** Now at this point we can start including our files, and creating the objects etc.
*** The first object were going to be including is the regsitry object.
**
*/

//System::
require_once APPLICATION_PATH "system/includes/classes/registry.php";

//Libary::
require_once APPLICATION_PATH "system/includes/libary/HTTPInput.class.php";

/*
**
*** As the object will be a "static" class we wont need to create an instance of the object
**
*/

?>
You will notice we have just added the require_once line
PHP Code: 
//Libary::
require_once APPLICATION_PATH "system/includes/libary/HTTPInput.class.php"
No this means that we have pulled the un registered class into the boot but it wont be
doing any thing unto we make and instance of the class so now were just going to add one line of code "after" the comment
PHP Code: 
/*
**
*** As the object will be a "static" class we wont need to create an instance of the object
**
*/ 
and were going to add

PHP Code: 
//Create the $Registry Object
$Registry = new Registry;

//Add Httpnput to the registry
$Registry->HttpInput = new HttpInput(); 
Ok so now we have a libary file thats used for accessing Http input globals such as $_GET and $_POST but we want to access them..

Well as you have loaded all this class objects etc etc before any real work in index.php this means where ever yu include the bootloader (boot.php) you have accesss to all your classes within 1 single variable called $Registry..

step 5. (Using the subclasses via Registry on yopur root pages)


so now if you if you would just like to run the scipt you should ge no errors atall... should be a blank page.

but now lets test the $Regsitry variable. within the index... ive added some more code to the index.php and heres what it looks like now

PHP Code: 
<?php

/*
**
*** First lets create a constant that we will be using for 2 main things.
*** 1. Used for security
*** 2. Used to help know where we am on the filesystem.
*** E.G. of the value is /root/sites/mySite/htdocs/
**
*/

define("APPLICATION_PATH" str_replace("\\","/",dirname(__FILE__)) . "/");

/*
**
*** No lets include the bootloader file
**
*/

require_once APPLICATION_PATH "system/boot.php";


/*
**
*** Anything below the inclusion of the system/boot.php should have all your libaries ready for usage!
**
*/


//lets do a test now.

$id $Registry->HttpInput->get("id");

echo 
$id// this outputs 7 when the uri is index.php?id=7
?>

as you can see by the ablove you have all your classes based around one registry system..
Now as we come to the end of this tutorial i would just like to point out the possibilities by
using a system like this.

I have a website running like this with some big libaries loaded into the registry such as

> Session Manager
> Database Wrapper
> Debug System
> PHP Speed logging class
> Smarty Template Engine
> Sub Database classes AKA Models (18 or more classes loaded)
> stdClass type DB config wrapper.

And with this system i have a bog/forum/directory and admin etc all running threw the primary Regsitry class...

And this is running a website with about 10-12K hits per day.... and is a very stable structure.

So if you are just learning PHP5-6 then you will need to know that the concepts i have showed yout today has only scratched the surface... PHP i believe is untapped potential and its only limited by the programmer. and i hope i have shared a nice tutorial here with you all

Files:
http://rapidshare.com/files/31192729...Files.zip.html
litewarez Reviewed by litewarez on . Advanced PHP (Registry System) - Guide/Tutorial Heya everyone, Introduction What you will learning to day is how to create a registry type of system for your "Framwork/Website" and how it will help you to manage all of your objects. What is a Registry you say? A registry is a class that will be used to hold other classes in an array/stdClass and create a type of "SperGlobal" for your script. The Steps. Rating: 5