[ create a new paste ] login | about

Link: http://codepad.org/aNW13yC9    [ raw code | output | fork ]

k4st - PHP, pasted on May 29:
<?php

error_reporting(E_ALL | E_STRICT);

/**
 * Class that becomes a super global variable.
 * @author Peter Goodman
 */
final class SuperGlobal implements ArrayAccess {
    public $globals = array();
    
    public function __construct(&$globals) {
        $this->globals = &$globals;
    }
    
    public function offsetGet($key) {
        return isset($this->globals[$key]) ? $this->globals[$key] : NULL;
    }
    public function offsetSet($key, $val) {
        echo 'hiii';
        $this->globals[$key] = $val;
    }
    public function offsetUnset($key) {
        unset($this->globals[$key]);
    }
    public function offsetExists($key) {
        return isset($this->globals[$key]);
    }
}

// create a new $GLOBALS array, populated with our new superglobals
$super_globals = array('_SERVER' => new SuperGlobal($_SERVER),
                       '_GET' => new SuperGlobal($_GET),
                       '_POST' => new SuperGlobal($_POST),
                       '_REQUEST' => new SuperGlobal($_REQUEST),
                       // ...
                       );

// overwrite the $GLOBALS array, then extract the new super globals by
// reference into the current scope, overwriting the shorthand to the
// normal superglobals.
$GLOBALS = &new SuperGlobal($super_globals);
extract($super_globals, EXTR_OVERWRITE | EXTR_REFS);

// function to test if the overwriting of the superglobals worked as
// expected
function test_scoping() {
    print_r($GLOBALS);
    print_r($_SERVER);
}

test_scoping();


Output:
SuperGlobal Object
(
    [globals] => Array
        (
            [_SERVER] => SuperGlobal Object
                (
                    [globals] => Array
                        (
                            [PHP_SELF] => t.php
                            [SCRIPT_NAME] => t.php
                            [SCRIPT_FILENAME] => t.php
                            [PATH_TRANSLATED] => t.php
                            [DOCUMENT_ROOT] => 
                            [REQUEST_TIME] => 1212023945
                            [argv] => Array
                                (
                                    [0] => t.php
                                )

                            [argc] => 1
                        )

                )

            [_GET] => SuperGlobal Object
                (
                    [globals] => Array
                        (
                        )

                )

            [_POST] => SuperGlobal Object
                (
                    [globals] => Array
                        (
                        )

                )

            [_REQUEST] => SuperGlobal Object
                (
                    [globals] => Array
                        (
                        )

                )

        )

)
SuperGlobal Object
(
    [globals] => Array
        (
            [PHP_SELF] => t.php
            [SCRIPT_NAME] => t.php
            [SCRIPT_FILENAME] => t.php
            [PATH_TRANSLATED] => t.php
            [DOCUMENT_ROOT] => 
            [REQUEST_TIME] => 1212023945
            [argv] => Array
                (
                    [0] => t.php
                )

            [argc] => 1
        )

)


Create a new paste based on this one


Comments: