[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[jfw] Re: Container and use of an alias



Check the container, it will have two object in the instances array, means it's only a matter for shared objects, if you take my example above and then do something like:
$this->container->get('MyApp\\Interfaces\\App\\AppResponseInterface')->setBody($data);
and in another function you do
$response = $this->container->get('yresponse');
you will have two different instances in the container.

Am Dienstag, 16. Dezember 2014 20:27:11 UTC+1 schrieb Don Gilbert:
Can you provide a test that proves this to be the case? In preliminary tests, this resolves as expected.

require 'vendor/autoload.php';


use Joomla\DI\Container;


$container = new Container;


$container->set('SomeRandomClass', new stdClass, true, true)->alias('classAlias', 'SomeRandomClass');


var_dump($container->get('SomeRandomClass'));

var_dump($container->get('classAlias'));


Dons-EE-MacBook-Air:test admin$ php index.php 

class stdClass#3 (0) {

}

class stdClass#3 (0) {

}


As you can see, they are the exact same class instance.


On Tuesday, December 16, 2014 9:27:19 AM UTC-6, Sven Versteegen wrote:
If you set a container key with alias for example:
$c->set('MyApp\\Interfaces\\App\\AppResponseInterface', [$this, 'getResponse'], true, true)
  ->alias('jresponse', 'MyApp\\Interfaces\\App\AppResponseInterface');
and you're using the long key in one function and the alias in another, the container will end up with 2 instances (long key/alias key) that maybe differ.
I expect to get the same instance here and to only get a new one if I set the second argument of the container get function(forceNew) to true,
is that a false expectation?
If not, adding $key = $this->resolveAlias($key); at the begining of the get method would solve that problem.

--
Framework source code: https://github.com/joomla-framework
Visit http://developer.joomla.org for more information about developing with Joomla!
---
You received this message because you are subscribed to the Google Groups "Joomla! Framework Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-framework+unsubscribe AT googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-framework.