Browse files

merged branch marijn/feature/constructor-arguments (PR #23)


2bf06b5 Added constructor arguments.


Added constructor arguments.

When creating dedicated containers with only a handful of parameters it can be nice to have constructor injection.


$pimple = new Pimple(array('secret' => 'fabp0t'));


by igorw at 2011-11-29T09:41:01Z

I don't see the need for this. Especially with silex extending it, I think it's better to keep the constructor empty. I can also see Pimple moving to a PHP 5.4 trait in the future (with a standard class using it), where defining the constructor would not make much sense.


by marijn at 2011-11-29T09:45:54Z

I'm not sure I follow your analogy: "*with silex extending it*". Could you elaborate?

I can understand it if you want to keep `Pimple` simple, however, I do think it's a valid use case...


by igorw at 2011-11-29T09:52:35Z

It was not an analogy at all, I'm referring to the "Silex\Application" class: extending the "Pimple" class.


by marijn at 2011-11-29T09:58:52Z

I understand, that's why I don't see what it has to do with this issue. I'm simply suggesting that `Pimple` (and indeed `Silex`) in some cases might benefit from constructor arguments.

As much as I'd love to see Pimple move to a trait, I'm not sure if anticipating for it at the moment is really viable.

Anyway, feel free to close this if you reckon this is too bloated :-)


by marijn at 2012-01-25T21:39:52Z

@fabpot do you have an opinion on this matter?

What can I do to make this merge-able?
  • Loading branch information...
2 parents f11c40f + 2bf06b5 commit 321db91e49b6cf8cbeed58d8db662d40de96d2c3 @fabpot committed Jan 26, 2012
Showing with 21 additions and 1 deletion.
  1. +13 −1 lib/Pimple.php
  2. +8 −0 tests/Pimple/Tests/PimpleTest.php
14 lib/Pimple.php
@@ -32,7 +32,19 @@
class Pimple implements ArrayAccess
- private $values = array();
+ private $values;
+ /**
+ * Instantiate the container.
+ *
+ * Objects and parameters can be passed as argument to the constructor.
+ *
+ * @param array $values The parameters or objects.
+ */
+ function __construct (array $values = array())
+ {
+ $this->values = $values;
+ }
* Sets a parameter or an object.
8 tests/Pimple/Tests/PimpleTest.php
@@ -97,6 +97,14 @@ public function testIsset()
+ public function testConstructorInjection ()
+ {
+ $params = array("param" => "value");
+ $pimple = new Pimple($params);
+ $this->assertSame($params['param'], $pimple['param']);
+ }
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Identifier "foo" is not defined.

0 comments on commit 321db91

Please sign in to comment.