# Global Hashmap Variable

**URL:** <https://discourse.slimframework.com/t/global-hashmap-variable/2482>\
**Category:** Questions\
**Created:** [June 19, 2018, 2:55am UTC](https://discourse.slimframework.com/t/global-hashmap-variable/2482 "2018-06-19T02:55:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcatlga](https://avatars.discourse-cdn.com/v4/letter/j/b4bc9f/32.png) [@jcatlga](https://discourse.slimframework.com/u/jcatlga)\
**Post date:** [June 19, 2018, 2:55am UTC](https://discourse.slimframework.com/t/global-hashmap-variable/2482/1 "2018-06-19T02:55:09Z")

</div>

I am trying to figure out how I can have a global variable, in this case a variable similar to a Java hashtable (or hashmap). For now I simply want to stick the userid and name in the global hashmap after each user login.  
Then lookup the name in some later code.  
The hashmap needs to be global singleton.  
I did some research and have 2 ideas below. What do you think?

// index.php (set global hashmap):  
$container = $app-\>getContainer();  
$container[‘allusers’] = function () {  
return array(); // singleton?  
};

// routes.php (after sign in set userid, name in hashmap):  
$container[‘allusers’][$userid] = $name;

// routes.php (other routes, get the user name)  
$uname = $container[‘allusers’][$userid]

* * *

$app-\>container-\>singleton(‘allusers’, function (){ // not sure if this works in slim 3?  
return array();  
});

$app-\>allusers[$userid] = $name;

$uname = $app-\>allusers[$userid];

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [June 19, 2018, 11:29am UTC](https://discourse.slimframework.com/t/global-hashmap-variable/2482/2 "2018-06-19T11:29:20Z")

</div>

By default, each time you get a service, [Pimple](https://github.com/silexphp/Pimple) returns the same instance of it (like a singleton).

Example:

```php
$container['session'] = function ($c) {
    return new Session(); // returns the same instance
};

```

> $app-\>container-\>singleton(‘allusers’, function (){  
> // not sure if this works in slim 3?

If you want a different instance to be returned for all calls, wrap your anonymous function with the factory() method.

```php
$container['session'] = $container->factory(function (Container $container) {
    return new Session();
});

```

> I am trying to figure out how I can have a global variable, in this case a variable similar to a Java hashtable (or hashmap)

You could use the Slim Collection class.

### Example container definition:

```auto
$container['allusers'] = function () {
    return new \Slim\Collection();
};

```

### Usage:

```php
/** @var \Slim\Collection $allUsers */
$allUsers = $container->get('allusers');

$allUsers->set('key', 'value');
var_dump($allUsers->all());

```

---

<div class="post-metadata">

**Author:** ![jcatlga](https://avatars.discourse-cdn.com/v4/letter/j/b4bc9f/32.png) [@jcatlga](https://discourse.slimframework.com/u/jcatlga)\
**Post date:** [June 20, 2018, 5:21am UTC](https://discourse.slimframework.com/t/global-hashmap-variable/2482/3 "2018-06-20T05:21:39Z")

</div>

I added the below code to dependencies.php:

$container[‘allusers’] = function () {  
return new \Slim\Collection();  
};

I can’t access the Collection in routes.php. I have tried the statements below. Getting undefined variable for the first two and a 500 error with no message for the 3rd.

$allUsers = $container-\>get(‘allusers’);

$allUsers = $app-\>getContainer()-\>get(‘allusers’);

$allUsers = $this-\>getContainer()-\>get(‘allusers’);

How do I access the Collection?

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [June 20, 2018, 7:53am UTC](https://discourse.slimframework.com/t/global-hashmap-variable/2482/4 "2018-06-20T07:53:46Z")

</div>

My code from above was just a generic example.

Within your `routes.php` try to access the container entry via `$this`.

Example (not testet):

```php
$allUsers = $this->allusers;

```

or like this:

```php
$allUsers = $this->get('allusers');

```

---

<div class="post-metadata">

**Author:** ![jcatlga](https://avatars.discourse-cdn.com/v4/letter/j/b4bc9f/32.png) [@jcatlga](https://discourse.slimframework.com/u/jcatlga)\
**Post date:** [June 21, 2018, 3:21am UTC](https://discourse.slimframework.com/t/global-hashmap-variable/2482/5 "2018-06-21T03:21:36Z")

</div>

> [@odan](#):
>
> $allUsers = $this-\>get(‘allusers’);

This gets the collection from the container. But it is getting a new and empty Collection each time. I was wanting to get the same Collection object. To build up the Collection over multiple requests.

The documentation says it should return a singleton. But each time I get it it is empty ???

The container definition is:

$container[‘allusers’] = function () {  
return new \Slim\Collection();  
};

The second question is, where does var\_dump() and print\_r() actually log to??

I am using wamp server and not seeing anything in the wamp logs or slim app log. ??

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [June 21, 2018, 8:14am UTC](https://discourse.slimframework.com/t/global-hashmap-variable/2482/6 "2018-06-21T08:14:05Z")

</div>

> But it is getting a new and empty Collection each time.

In PHP a singleton is only the same within the same request/response cycle.

> To build up the Collection over multiple requests.

Well, this is not how PHP works. PHP is stateless (like the web). Everything’s is getting rebuilt and/or reloaded on each request. Unfortunately the only way to remotely have some kind of stateful processing in PHP is by using of sessions (and indirectly cookies). And even if you use the session to save your state it will have to be rebuilt on the next request.

---

<div class="post-metadata">

**Author:** ![jcatlga](https://avatars.discourse-cdn.com/v4/letter/j/b4bc9f/32.png) [@jcatlga](https://discourse.slimframework.com/u/jcatlga)\
**Post date:** [June 22, 2018, 3:21am UTC](https://discourse.slimframework.com/t/global-hashmap-variable/2482/7 "2018-06-22T03:21:58Z")

</div>

Ok got it. The other thing was, where does var\_dump() and print\_r() actually log to?

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [June 22, 2018, 7:14am UTC](https://discourse.slimframework.com/t/global-hashmap-variable/2482/8 "2018-06-22T07:14:59Z")

</div>

`var_dump()` and `print_r()` are PHP internal functions to print the content of variables. By default the output goes directly into the [output buffer](http://php.net/manual/en/book.outcontrol.php). Some people use this functions for debugging 😉 Slim is based on [PSR-7](https://www.php-fig.org/psr/psr-7/) and the output is handled internally via [Resources](http://php.net/manual/en/language.types.resource.php). That’s why you should use the [Slim Response](https://www.slimframework.com/docs/v3/objects/response.html) ($response) object to generate an output for the client. I would recommend [Monolog](https://akrabat.com/logging-errors-in-slim-3/) for logging.
