3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class Mailer{ } class Container { static protected $shared = array(); protected $parameters = array(); public function __construct(array $parameters = array()) { $this->parameters = $parameters; var_dump($parameters); } // ... public function getMailer() { if (isset(self::$shared['mailer'])) { return self::$shared['mailer']; } $class = $this->parameters['mailer.class']; $mailer = new $class(); return self::$shared['mailer'] = $mailer; } } $container = new Container(array( 'mailer.username' => 'foo', 'mailer.password' => 'bar', 'mailer.class' => 'Mailer', )); $mailer = $container->getMailer(); var_dump($mailer);
based on NZYRH
Output for 5.6.0 - 7.3.0alpha1
array(3) { ["mailer.username"]=> string(3) "foo" ["mailer.password"]=> string(3) "bar" ["mailer.class"]=> string(6) "Mailer" } object(Mailer)#2 (0) { }