Skip to content
WJunction - Webmaster Forum

Tutorial [Creating a PHP Framework] {advanced} (PART 3)

Status
Not open for further replies.
Part 2: http://www.wjunction.com/showthread.php?t=24411

OK so heres part 3 everyone!

In part to we discussed Input and why it is used, today were going to be discussing Output..

So the output side of things will handle all data sent to the user, such as cookies, headers, html and it will also set the compression if needed!

were only going to set up a basic ouput object at first with encoding,headers, and cookies! if you wish you could go more advanced but we keep it simple at the start!

Ok so lets create a new file called Output.php!

in the code below ive left comments so you know whats going on!

PHP:
<?php
if(!defined('BASE_PATH')){exit;} /*REMEMBER this should be in every file thats included by startup.php*/

class Output
{

	private $headers = array(); //This will hold all the headers in
	private $compression_level = 0; //This will hold the compression level
	private $output = ''; // this will be used to hold the output data.
	private $callbacks = array(); // this will hold any callbacks you wish to perform before the data is outputted!
	
	function __construct()
	{
		//Do nothing in the construct!
		//unless you want to add some global headers like content-type but i dont advise it as you will be using this class for all outputs such as xml,js,json,html etc
	}
	
	/*
		This function will allow you to add callbacks su as $this->addCallback('htmlenities') or $this->addCallback(array($object,'method')) ;
	*/
	
	public function addCallback($callback){
		//Ok we need to check if the callback is a string or array, if its a string we treat it as function else if its an array we treat as method of an object
		switch(gettype($callback))
		{
			case 'string':
				is_callable($callback,false,$callable) ? $this->callbacks[] = $callable : trigger_error('Callback not available ({$callback})',E_USER_ERROR);
			break;
			case 'array':
				//The second param tells php its an object
				is_callable($callback,true,$callable) ? $this->callbacks[] = $callable : trigger_error('Callback not available ({$callback})',E_USER_ERROR);
			break;
		}
		//Ok so if you ever get errors from here make sure your functions/methods are callable
	}
	
	/*
		This function will be used to add headers to the array
	*/
	public function addHeader($name,$value){
		$this->headers[$name] = $value; //Simple eh
	}
	
	public function addCookie($key,$value,$expire = 604800,$path = '/',$domain = '',$secure = false,$httponly = true)
	{
		 setcookie($key,$value,$expire,$path,$domain,$secure,$httponly);
	}
	
	/*
		This will remove any set headers!
	*/
	public function removeHeader($name)
	{
		if (isset($this->headers[$name]))
		{
			unset($this->headers[$name]);
		}
	}
	
	/*
		This will do a redirect with headers and exit just after
	*/
	public function redirect($url)
	{
		header('Location: ' . $url);
		exit;
	}
	
	public function setCompression($level)
	{
		$this->compression_level = (int)$level;
	}
	
	/*
		This send function will take the html you wish to output and perform compression, headers, and initial output!
	*/
	
	public function send($contents)
	{
		//Set the variable in the class
		$this->output = $contents;
		
		//we going to run the callbacks on the output
		foreach($this->callbacks as $this->callback)
		{
			call_user_func($this->callback,&$this->output); //we use the ampersand to tell php to directly save it on the inputted variable
		}
		
		//Lets check to see if we have a compression leve
		if($this->compression_level)
		{
			//we compress with the private::compress function
			$this->output = $this->compress($contents,$this->level);
		}
		
		//Ok so now we may have headers so we need to send these before we send the data/html
		if(!headers_sent())
		{
			//Aslong as the headers are not set we then loop the keys and values and send them via header();
			foreach ($this->headers as $key => $value)
			{
				header($key . ': ' . $value);
			}
		}
		//So now we just have to echo the output
		echo $this->output;
	}

	private function compress($data, $level = 0) 
	{
		if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE))
		{
			$encoding = 'gzip';
		} 

		if(isset($_SERVER['HTTP_ACCEPT_ENCODING']) && (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'x-gzip') !== FALSE))
		{
			$encoding = 'x-gzip';
		}

		if(!isset($encoding))
		{
			return $data;
		}

		if(!extension_loaded('zlib') || ini_get('zlib.output_compression'))
		{
			return $data;
		}

		if(headers_sent())
		{
			return $data;
		}

		if(connection_status())
		{ 
			return $data;
		}
		
		$this->addHeader('Content-Encoding', $encoding);

		return gzencode($data, (int)$level);
	}
}
?>

if your finding it hard to understand how this will work and why its so important then just ask....


Basically so far with what we have you will use the Input to grab what you need! then you will use the database to fetch content, then you will assign it to a template manager and then send the compiled data to the Output with compression and other headers

well thats the principle any way!

Ok so we need to include this into the framework but first

Save the file in /system/engine/Output.php

Then open up the startup.php file and lets include the file!

FInd:
PHP:
include SYSTEM_BASE_PATH . '/engine/Input.php';

Add after:
PHP:
include SYSTEM_BASE_PATH . '/engine/Output.php';


Now lets add it to the Registry system!

Find:
PHP:
Registry::set('Input', new Input());

Add after:
PHP:
Registry::set('Output', new Output());

And that it for Part 3!

Part 4 we will be looking at a database extraction layer and how we can use this to perform fast query such as 1 line assoc loops!
 

1 comment

will this help me to get a girlfriend im confused ? :S ?

na im joking been wanting to learn php will sit down one day a read through these should help me thanks lite
 
Status
Not open for further replies.

About the author

L
Active Member · Joined
I am a skilled programmer within PHP,HTML,CSS,MYSQL and also the owner of several websites Programming, Football...
1,367
Messages
1
Reactions
38
Points

Advertise on WJunction

Reach 1000's of webmasters, hosts & affiliates. Banner & sponsored-thread slots available.

Contact us
Back
Top Bottom