<?php

final class Request {

	// object instance
    private static $instance;
    
    /**
     * In this var are the parameters stored
     *
     * @var array
     */
    static private $params = array();
    
	/**
	 * prevent direct access and cloning
	 * 
	 */
    private function __construct(){}
    private function __clone(){}

	/**
	 * create an instance of self
	 *
	 * @return Request
	 */
    public function getRequest() {
        if (!self::$instance instanceof self) {
            self::$instance = new self();
            self::parseUrl();
        }
        return self::$instance;
    }
    
    /**
     * This method will parse the url and put it as an array
     * in self::$params.
     *
     */
    private function parseUrl(){
		$pathInfo = explode('/', trim($_SERVER['PATH_INFO'], '/'));
		
		$myPath = array();
		for($i=0, $count=count($pathInfo); $i+1<$count && $count>1; $i+=2) {
			$myPath[(string) $pathInfo[$i]] = (string) $pathInfo[$i+1];
		}
		self::$params = $myPath;
    }
    
    /**
     * This function will return one of the items 
     *
     * @param string $key 
     * @param mixed $default
     * @return string/mixed. 
     */
    public function getParam($key, $default = null){
		$key = (string) $key;
    	if(isset(self::$params[$key])){
			return self::$params[$key];
		}
    	return $default;
	}
    
	/**
	 * A magic method wich does the same as self::getParam()
	 *
	 * @param string $key
	 * @return string
	 */
	public function __get($key){
		return self::getParam($key);
	}    
    
	/**
	 * This method will return all the values of self::$params
	 *
	 * @return unknown
	 */
    public function getParams(){
    	return self::$params;
    }
}

var_dump(Request::getRequest()->index);
var_dump(Request::getRequest()->getParams());

?>