Thanks! Finally decided to do it like that:
namespace MyLibs;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use \InvalidArgumentException;
class ServiceLocatorProvider implements ServiceLocatorAwareInterface
I use it in this way:
$serviceProvider = ServiceLocatorProvider::getInstance();
$serviceProvider->setServiceLocator(new ServiceManager());
$serviceProvider->getServiceLocator()->setService('dbAdapter', $adapter);
Regards,
S.
На 14.12.2012 00:32, Marco Pivetta написа:
> If your instance comes from a Service Manager itself, you can handle its
> instantiation as you like.
> Basically, this means that you shouldn't create an instance yourself, but
> instead always fetch one from the service manager.
>
> Marco Pivetta
>
> http://twitter.com/Ocramius
>
> http://ocramius.github.com/
>
>
>
> On 13 December 2012 17:27, saiman <[email protected]> wrote:
>
>> Thanks for the replay,
>>
>> I checked ServiceLocatorAwareInterface. As I understand it's applicable
>> only for the controllers? Just wondering who is going to inject the
>> ServiceManager to my class if it's not related with the ZF2 (I don't use
>> the Zend MVC) at all.
>> In general I've got something like the structure bellow:
>>
>> abstract class myDbRowClass
>> class MyFirstLibrary extends myDbRowClass
>> class MySecondLibrary extends myDbRowClass
>>
>> I need the database almost everywhere in those libraries including
>> static methods. Sounds confusion to pass it on the constructor. Was
>> looking another solutions related to DependencyInjection. Something like
>> this one bellow:
>>
>> $di = new \Zend\Di\Di();
>> $di->configure(new \Zend\Di\Configuration(array(
>> 'definition' => array(
>> 'class' => array(
>> 'My\\UserTable' => array(
>> 'setDatabaseAdapter' => array('required' => true)
>> )
>> )
>> )
>> )));
>> $userTable = $di->get('My\\UserTable',
>> array('dsn'=>'mysql:host=server2;dbname=somedb'));
>>
>>
>> Here I don't like the idea to pass the second parameter -
>> array('dsn'=>'mysql:host=server2;dbname=somedb') every time when I want
>> to instantiate an object. I'm just wondering the "the right way" to that
>> with ZF2?
>>
>> Regards,
>> S.
>>
>>
>> ?? 13.12.2012 17:48, Marco Pivetta ??????:
>>> Heya,
>>>
>>> No, you have to keep a reference to the service manager.
>>> The whole point of doing that is to avoid having singletons or
>>> global/static variables that would pollute the environment and make
>> things
>>> such as 2 ServiceManagers with different services very very hard to
>> achieve.
>>> I suggest you to pass the service manager (at instantiation time) to
>>> whatever object should be using it (or even better just pass what the
>>> object really needs to the object).
>>>
>>> You can read up more about it on
>>>
>> http://framework.zend.com/manual/2.0/en/modules/zend.service-manager.quick-start.htmlin
>>> section "Creating a ServiceLocator-aware class"
>>>
>>> Marco Pivetta
>>>
>>> http://twitter.com/Ocramius
>>>
>>> http://ocramius.github.com/
>>>
>>>
>>>
>>> On 13 December 2012 16:32, saiman [via Zend Framework Community] <
>>> [email protected]> wrote:
>>>
>>>> Hi all,
>>>> Is it possible to create one instance of the service manager and use it
>>>> everywhere?
>>>> For example I want to do following in my index.php:
>>>>
>>>> $serviceManager = new ServiceManager();
>>>> $serviceManager->setService('dbAdapter', $adapter);
>>>>
>>>> And then somewhere in my project (OneOfMyLibraries.php) to get the
>>>> database instance, like:
>>>>
>>>> $serviceManager = new ServiceManager();
>>>> $dbh = $serviceManager->get('dbAdapter');
>>>>
>>>> Unfortunately this is not possible the second instance it's not related
>>>> wit the first one. Any idea how to proceed or what to use?
>>>>
>>>> Regards,
>>>> S.
>>>>
>>>>
>>>> ------------------------------
>>>> If you reply to this email, your message will be added to the
>> discussion
>>>> below:
>>>>
>>>>
>> http://zend-framework-community.634137.n4.nabble.com/ZF2-Using-ServiceManager-tp4658461.html
>>>> To start a new topic under Zend Framework, email
>>>> [email protected]
>>>> To unsubscribe from Zend Framework Community, click here<
>> http://zend-framework-community.634137.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=634137&code=b2NyYW1pdXNAZ21haWwuY29tfDYzNDEzN3wxNzE0OTI1MTk4
>>>> .
>>>> NAML<
>> http://zend-framework-community.634137.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml
>>>
>>>
>>> --
>>> View this message in context:
>> http://zend-framework-community.634137.n4.nabble.com/Re-ZF2-Using-ServiceManager-tp4658466.html
>>> Sent from the Zend Framework mailing list archive at Nabble.com.
>>
--
List: [email protected]
Info: http://framework.zend.com/archives
Unsubscribe: [email protected]