Gajus
Gajus

Reputation: 73858

custom super global in PHP

Is it possible to define a custom super global variable? (whether in code, or using php.ini)

Example, for all projects I use a custom framework. The framework essentially stores all data about running instance of the script (template loaded, template variables, etc.) in a single variable. I'd like that variable to become cross-system accessible.

I am perfectly aware of $_GLOBALS and global, however the question is asking if it is possible to define custom super global variable, e.g. $foo, which would become accessible by the same name in any scop.

Upvotes: 1

Views: 2249

Answers (6)

Piero Mori
Piero Mori

Reputation: 459

Sorry, but all answers are wrong. Correct answer: yes, it is possible, but not with the so-called "core" PHP functionalities. You have to install an extension called runkit: http://www.php.net/manual/en/runkit.installation.php

After that, you can set your custom superglobals in php.ini as documented here: http://www.php.net/manual/en/runkit.configuration.php#ini.runkit.superglobal

Upvotes: 3

infinito84
infinito84

Reputation: 2085

My solution

Really php dont support to define more superglobals but if you want share vars between differents users and sessions, my solution is to create a unique session for save shared information. The proccess consist in to close current session, open the shared session to write and read and back in to the previous session.

Code:

<?php
    ini_set('display_errors',1);
    error_reporting(E_ALL);

    function get_global($key){
        //Get current session
        if(session_status()!=PHP_SESSION_ACTIVE)session_start();
        $current_id=session_id();
        session_write_close();
        //Set a global session with session_id=1
        session_id(1);
        session_start();
        //Get superglobal value
        $value=null;
        if(isset($_SESSION[$key]))$value=$_SESSION[$key];
        session_write_close();
        //Set the before session
        session_id($current_id);
        session_start();
        return $value;
    }

    function set_global($key,$value){
        //Get current session
        if(session_status()!=PHP_SESSION_ACTIVE)session_start();
        $current_id=session_id();
        session_write_close();
        //Set a global session with session_id=1
        session_id(1);
        session_start();
        //Set superglobal value
        $_SESSION[$key]=$value;
        session_write_close();
        //Set the before session
        session_id($current_id);
        session_start();
    }
    //Example 
    //Begin my session normally
    session_start();
    if(empty($_SESSION['count'])){
        $_SESSION['count']=0;
        $_SESSION['color']="rgb(".rand(0,255).",".rand(0,255).",".rand(0,255).")";
    }
    $_SESSION['count']++;
    $id=session_id();
    //Get the superglobal
    $test=get_global("test");
    //Set the superglobal test with empty array if this dont set
    if($test==null)$test=array();
    //Get the superglobal
    $test=get_global("test");
    //Set values for each reload page and save the session_id that create it
    $test[]="<span style='color:".$_SESSION['color']."'>Value: ".rand(0,100)." SessionID: $id</span><br>";
    //Save the superglobal
    set_global("test",$test);
    //Show the superglobal
    foreach($test as $t){
        echo $t;
    }
    echo "<b>Reloads = ".$_SESSION['count'].", <span style='color:".$_SESSION['color']."'>This my color</span></b>";

    exit;
?>

Test:

In this example $test is superglobal var that contain array with random number and session_id that created it. Each session defines two local variables for color text and count reloads..

Result my solution

Upvotes: 0

kivlara
kivlara

Reputation: 63

A little hack(trick) that can be used as superglobals, it is a singletone like implementation but I am agree with @Matt Esch, don't use superglobals ...

class K
{
    public $CONST = array();

    private static $_instance = null;

    private function __construct()
    {

    }

    protected function __clone()
    {

    }

    static public function I()
    {
        if(is_null(self::$_instance))
        {
            self::$_instance = new self();
        }

        return self::$_instance;
    }
}

then you can use this in all methods, class, functions, like a superglobal var.

K::I()->CONST[0] = "somevar";
K::I()->CONST[1] = array(1, 2, 3, 4, 5);

Upvotes: 1

Matt Esch
Matt Esch

Reputation: 22956

I'm not offering a solution to this problem as such, but I suggest that you avoid using globals. Generally speaking use of globals is considered bad practice, even in programming languages that make use of them by design. You cannot be sure how your global will affect other applications that declare the same variable. Personally I would prefer a more managed approach to retrieving data, either specifically from another php script or by writing a php extension that defines a new function and returns the data you want. It's not unusual to store application settings in a database, be that MySQL or flat file text, and would be my preferred method for sharing information cross-application.

Upvotes: 2

RCE
RCE

Reputation: 1721

Sadly there is no way to define superglobals.

(There is no mechanism in PHP for user-defined superglobals.)

Source

Upvotes: 9

Evert
Evert

Reputation: 99599

This is not possible, and also bad design. (as are super globals).

If you do think global state is the answer for you, you could use static classes.

Upvotes: 2

Related Questions