Results 1 to 10 of 22
-
25th Nov 2009, 09:25 AM #1OPMemberWebsite's:
litewarez.net litewarez.com triniwarez.comAdvanced 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.
- 1. Create index.php
- 2. Create create a bootloader
- 3. Create the registry
- 4. Create create some sub-classes
- 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";
?>
What the bootloader will be doing in this "system" is
- 1. including all the main class files[/*]
- 2. Creating an instance of the registry object[/*]
- 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
**
*/
?>
[*]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);
}
}
?>
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
}
}
?>
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
**
*/
?>
PHP Code://Libary::
require_once APPLICATION_PATH . "system/includes/libary/HTTPInput.class.php";
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
**
*/
PHP Code://Create the $Registry Object
$Registry = new Registry;
//Add Httpnput to the registry
$Registry->HttpInput = new HttpInput();
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.htmllitewarez 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: 5Join Litewarez.net today and become apart of the community.
Unique | Clean | Advanced (All with you in mind)
Downloads | Webmasters
Notifications,Forum,Chat,Community all at Litewarez Webmasters
-
25th Nov 2009, 01:43 PM #2Member
yea really good tutorial.
php has endless of possiblities
-
25th Nov 2009, 02:15 PM #3MemberWebsite's:
warezxtc.comGod bless.
-
25th Nov 2009, 02:23 PM #4OPMemberWebsite's:
litewarez.net litewarez.com triniwarez.comlol why god bless and thanks Deleted
Join Litewarez.net today and become apart of the community.
Unique | Clean | Advanced (All with you in mind)
Downloads | Webmasters
Notifications,Forum,Chat,Community all at Litewarez Webmasters
-
25th Nov 2009, 02:27 PM #5MemberWebsite's:
warezxtc.com
-
25th Nov 2009, 02:44 PM #6OPMemberWebsite's:
litewarez.net litewarez.com triniwarez.comooooh yea..... lol
Join Litewarez.net today and become apart of the community.
Unique | Clean | Advanced (All with you in mind)
Downloads | Webmasters
Notifications,Forum,Chat,Community all at Litewarez Webmasters
-
25th Nov 2009, 02:46 PM #7MemberWebsite's:
warezxtc.com
-
25th Nov 2009, 02:48 PM #8OPMemberWebsite's:
litewarez.net litewarez.com triniwarez.comhahahahha made me lol
Join Litewarez.net today and become apart of the community.
Unique | Clean | Advanced (All with you in mind)
Downloads | Webmasters
Notifications,Forum,Chat,Community all at Litewarez Webmasters
-
25th Nov 2009, 02:50 PM #9MemberWebsite's:
warezxtc.com
-
25th Nov 2009, 02:54 PM #10Member
lmfao wow cyb
Sponsored Links
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
-
Advanced Bash Loops Tutorial
By Albert.Nawaro in forum Tutorials and GuidesReplies: 0Last Post: 9th Feb 2012, 10:56 AM -
[AMS] xCleaner - Cleans your System & Registry ;)
By l0calh0st in forum Web Development AreaReplies: 1Last Post: 24th Jul 2010, 05:10 PM -
Tutorial [Creating a PHP Framework] {advanced} (PART 3)
By litewarez in forum Tutorials and GuidesReplies: 1Last Post: 4th Feb 2010, 02:12 AM -
Tutorial [Creating a PHP Framework] {advanced} (PART 2)
By litewarez in forum Tutorials and GuidesReplies: 2Last Post: 3rd Feb 2010, 05:37 AM
themaRegister - register to forums...
Version 3.54 released. Open older version (or...