phppimple

Pimple is always returned as an array


I have this trait

use Pimple\Container;

trait ContainerAwareTrait
{
    protected $container;

    public function setContainer(Container $container = null)
    {
        if (null !== $this->container) {
            $this->container = $container;
        }
    }

    public function getContainer()
    {
        return $this->container;
    }
}

I have this code using it (via an App class)

$container = new Pimple\Container();
$app = new App($container); // uses ContainerAwareTrait, constructor calls `setContainer`
var_dump($app->getContainer(), $container);die;

What I'm seeing is that the first var_dump result is an array, and the second is an instance of Pimple\Container.

I was expecting them both to be the same instance. Can someone tell me what is going on here?


Solution

  • Your setter logic is wrong: you want to set this->container when it hasn't been set (is null), not when it is already set (is not null).