I want to set up a service, so that I get a new instance of a particular class, every time I instantiate from the DI. So, the objects should not share data.
I would do that in my services.php like this, without true
as a 3rd parameter in the set()
method:
$di->set('testclass', function () {
return new \EE\Library\Inputs\TestClass();
});
But, I found that a second instance of the class does share data with the first instance, which it shouldn't. When I do this:
$test1 = $this->di['testclass'];
$test1->setValue('test 1');
$test2 = $this->di['testclass'];
echo $test2->getValue();
The value test 1 is displayed, while nothing should be. The class is this:
namespace EE\Library\Inputs;
class TestClass
{
protected $value = '';
public function setValue($new) {
$this->value = $new;
}
public function getValue() {
return $this->value;
}
}
Am I doing something wrong? I checked that it didn't make a difference when I added the parameter true
in the set()
method.
(Phalcon version is 2.0.9)