Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access a global object inside a method closure

I currently have a dependency injection module which allows me to create a factory of objects:

class DiModule
{
    private $Callbacks;

    public function set(
        $foo,
        $bar
    ) {
        $this->Callbacks[$foo] = $bar;
    }

    public function get(
        $foo
    ) {
        return $this->Callbacks[$foo];
    }
}

I then have an event object which stores a method closure and the session that will trigger the event.

class Event
{
    private $Sesh;
    private $Method;

    public function set(
        $sesh = array(),
        $method
    ) {
        $this->Sesh = $sesh;
        $this->Method = $method;
    }

    public function get(
    ) {
        return [$this->Sesh,$this->Method];
    }
}

I then have a listener object which searches the sessions set and triggers the event associated with that object.

class Listener
{
    private $Sesh;
    public function setSesh(
        $foo
    ) {
        $this->Sesh = $foo;
    }

    private $Event;
    public function set(
        $foo,
        Event $event
    ) {
        $this->Event[$foo] = $event;
    }

    public function dispatch(
        $foo
    ) {
        $state = true;

        if(isset($this->Event[$foo]))
        {
            foreach($this->Event[$foo]->get()[0] as $sesh)
            {
                if(!isset($this->Sesh[$sesh]) || empty($this->Sesh[$sesh]))
                {
                    $state = false;
                }
            }
        }

        return ($state) ? [true, $this->Event[$foo]->get()[1]()] : [false, "Event was not triggered."];
    }
}

This is an example of this being executed

$di = new DiModule();

$di->set('L', new Listener());
$di->set('E', new Event());

$di->get('E')->set(['misc'], function () { global $di; return $di; });

$di->get('L')->setSesh(array('misc' => 'active')); // not actual sessions yet
$di->get('L')->set('example', $di->get('E'));
var_dump($di->get('L')->dispatch('example'));

The issue is when I try to access my global $di inside a closure, I have googled this numerous times but cannot find a solution.

like image 628
Jaquarh Avatar asked Sep 30 '16 11:09

Jaquarh


People also ask

Can global variables be accessed anywhere?

Variables that are created outside of a function are known as Global Variables . A global variable is one that can be accessed anywhere . This means, global variable can be accessed inside or outside of the function.

Are global functions closures?

Global functions are closures that have a name and don't capture any values. Nested functions are closures that have a name and can capture values from their enclosing function. Closure expressions are unnamed closures written in a lightweight syntax that can capture values from their surrounding context.

What is the name of the object to access global variables from inside a script?

Accessing a Global VariableThe window is the global object in the browser environment. Any property attached to the window object can be accessed from any script on the web page, including the script for the React app. Since window is a global object, the React code can also access its properties, as shown below.

What object is used to access globally scoped variables in a browser for a front end web application?

The global object in JavaScript is an always defined object that provides variables and functions, and is available anywhere. In a web browser, the global object is the window object, while it is named global in Node. js. The global object can be accessed using the this operator in the global scope.


1 Answers

You need to use the use keyword to access an outside variable from within a closure.

So this:

$di->get('E')->set(['misc'], function () { global $di; return $di; });

Should be written like this:

$di->get('E')->set(['misc'], function () use ($di) { return $di; });
like image 55
laurent Avatar answered Oct 04 '22 20:10

laurent