• Log in or sign up for an account here: https://www.sythe.org/login

[Creating a PHP Framework] - PART 1

EXT

Active Member
Joined
Apr 23, 2008
Sythe Gold
0
Messages
117
Location
Australia
[Creating a PHP Framework] - PART 1
I thought i would do a tutorial on creating a PHP framework so you can build web applications with ease!

What is a PHP framework?
The best way i can describe in laymen terms is a framework is the foundations/scaffolding of your application, the framework consists of things like database abstraction layers, input and output controlling, security etc.

when the framework is built you will be able to use the code to do stuff in your application like fetch database data, get inputted data and many many more.

Okay lets get started:

The first page were going to create is index.php, the index is what we call a view page.. the reason we call it a view page is its one of the apges that a user will be able to goto to view the data.

In the PHP file were going to have to create a few lines of code!

1. Define a constant (used for security and relative paths)
2. include the mian startup file! (this file loads all companents of the framework)


Here's the code

PHP:
<?php

//heres the define
define('BASE_PATH',str_replace("\\","/",dirname(__FILE__)));

/*
*   What the define does is crate a type of variable that can be used ANYWHERE within the script
*   Where also replacing \ with / withing the path were in! this makes it easy for for the script to work on windows or linux
******
*    Exmaples of what BASE_PATH contains
*    Windows: c:/xampp/htdocs
*    Linux: /home/web/htdocs
*/

include BASE_PATH . '/system/startup.php';

/*
*  Include the main file
*/
?>
Ok so whats in index.php is the basic NEEDED lines to add new files, so for instance if we was to create another file call downloads.php we would add them lines in that file to load all the core framework components.

So now we need to make a folder next to index.php called system!, this is where all our framework files will be included!

ok so now we have the folder called system were going to create the startup.php and save it in that folder!

About the startup.php

* Will check to make sure that index.php is the file thats called it
* Will check server settings for compatibilty
* Will include all mojor framwork classes
* Assign all framwork objects to a registry
* perform basic stuff before you start using it to create a website


The startup.php file

PHP:
<?php

if(!defined('BASE_PATH')){exit;}
/*
* This will check to see BASE_PATH is defined... if its not it will niot run anything from here! if you remember we have defined BASE_PATH within index.php so this is ok as long as its index.php thats called it!
* This will make sure that a hacker cant directly inject the file with bad code!
*/

//Set error reporting
error_reporting(E_ALL);
//This is we can track any little errors php causes during execution

// Check Version
if(version_compare(phpversion(), '5.1.0', '<') == TRUE){exit('PHP5.1+ Required');}
//As were using classes, objects,statics etc it would be best to make sure were in PHP 5.1 or greater

//Define SYSTEM_BASE_PATH
define('SYSTEM_BASE_PATH',str_replace('\\','/',dirname(__FILE__)));
//This is the same as index.php but defines THIS folder aka system

/*
** here we will include the Registry file
*/

/*
** here we will include all framework files
*/

/*
** here we will load all classes into the registry
*/

/*
** here we will perform basic startup stuff like config etc
*/
?>
As you can read from above ive left some comments as i dont want to fill everything in when im not sure what im going to code yet!

but we are going to create a registry object!

What is a registry object
Ok so back to laymen terms now, A registry object is like a box and this box stores all our code.. now in php theres a thing called Scope! this is is pretty annoying but its how the structure works, so were going to make this box special! were going to make it available everywhere in the application.. this way we dont have to grab each different class object! we gram the 1 big one and we instantly have access to every component in the framework

ok so within the system folder were going to create a new folder called 'engine'... this folder will store all the MAIN topend code thats used to glue everything together as such!

ok so we should have a folder in system called engine.. withing that folder were going to create a new php file called registry.php

it should look like this

** /system/engine/registry.php

And heres the code the that file!
PHP:
<?php
//All files included by any file thats used with index should have the belop line
if(!defined('BASE_PATH')){exit;}

final class Registry {
    static private $data = array();

    static public function get($key){
        return (isset(self::$data[$key]) ? self::$data[$key] : NULL);
    }

    static public function set($key, $value){
        self::$data[$key] = $value;
    }

    static public function has($key){
        return isset(self::$data[$key]);
      }
}
?>
Now this code may be a little advanced for you at the moment but basically notice the keywords such as static and self:: ... these allow me to use these functions without creating an instance of the object..

Do some research if you're not sure!

ok now in the startup.php file i want you to include the registry file so replace

PHP:
/*
** here we will include the Registry file
*/
with

PHP:
include SYSTEM_BASE_PATH . '/engine/registry.php';
ok so this is the end of part one!

PLEASE DO NOT START THIS CODE UNTIL I HAVE FINISHED ALL SECTIONS.

in part 2 were going to cover the following if possible

* Creating the first component (Input)
* Adding the component to the registry
* Possibly add in Request component!

Part 2 link: (Not yet finished).

Thanks for reading,
EXT
 
Runescape Gamblingrunescape gambling
Back
Top