Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9226075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:44:28+00:00 2026-06-18T04:44:28+00:00

I’m simply looking for advice on the best way I should handle this situation.

  • 0

I’m simply looking for advice on the best way I should handle this situation.

Right now I’ve got several files in a folder called Service. The files contact several functions which do random things of course. Each of these files needs access to the SM Adapter.

My question is, should I implement the ServiceManagerAwareInterface in each of these files OR should I just make a new class which implements the ServiceManagerAwareInterface and just extend my classes on the new class which implements this service?

Both ways work as they should, just not sure which way would be more proper.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:44:30+00:00Added an answer on June 18, 2026 at 4:44 am

    If you think that your system will always rely on ZF2, both approaches are equivalent.

    Now from an OO design perspective, personally I have a preference for the approach in which you extend your service then implement the ServiceManagerAwareInterface. I would even use an interface for the dependency over the ServiceLocator to protect even more my classes. Why?
    Extending your classes does not cost you a lot, same for making your class depending on interfaces.

    Let’s take this example, Imagine you did not use this approach during a ZF1 project, during which you had probably resolved your dependencies with the Zend_Registry.
    Now, let’s assume you moved to a ZF2 implementation, how much time you think you’ll spend refactoring your code from something like Zend_Registry::get($serviceX) to $this->getServiceManager()->get($serviceX) on your Service layer?

    Now Assume you had made the choice of protecting your classes, first by creating your own Service locator interface, as simple as:

    public interface MyOwnServiceLocatorInterface{
        public function get($service);
    }
    

    Under ZF1 you had created an adapter class using the Zend_Registry:

    public class MyZF1ServiceLocator implements MyOwnServiceLocatorInterface{
        public function get($service){
            Zend_Registry::get($service);
        }
    }
    

    Your Service classes are not coupled to the Zend_Registry, which make the refactoring much more easier.

    Now, You decide to move to ZF2 so you’ll logically use the ServiceManger. You create then this new Adapter class:

    public class MyZF2ServiceLocator implements 
         ServiceManagerAwareInterface,MyOwnServiceLocatorInterface
    {
    
        private $_sm;
    
        public function get($service){
            $this->_sm->get($service);
        }
    
        public function setServiceManager($serviceManager){
             $this->_sm = $serviceManager;
        }
    }
    

    Again, your Service classes are not coupled to the ZF2 ServiceManger.

    Now, how would look like the configuration/registration of you Service layer on the ServiceManager. Well, you’ll use your Module::getServiceConfig class for that:

    //Module.php
    
    public function getServiceConfig()
    {
         return array(
               'factories'=>array(
                    'My\ServiceA'=>function($sm){
                            return new My\ServiceA($sm->get('My\Service\Name\Space\MyZF2ServiceLocator'));
                        }
    
                //Some other config
    
                )
    }
    

    As you can see, no refactoring is needed within your Service classes as we protected them by relying on interface and using adapters. As we used a closure factory, we don’t even need to extend our Service classes and implement the ServiceLocatorAwareInterface.

    Now, before concluding in my previous example i have to note that I did not treat the case in which my classes are constructed via factories, however, you can check one of my previous answers that address the factory topic but also the importance of loose coupling among an application layers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
I have a jquery bug and I've been looking for hours now, I can't
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.