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 8089813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:31:24+00:00 2026-06-05T19:31:24+00:00

assumption: Event\Service\EventService is my personal object that works with Event\Entity\Event entities This code works

  • 0

assumption: Event\Service\EventService is my personal object that works with Event\Entity\Event entities

This code works in an ActionController:

$eventService = $this->getServiceLocator()->get('Event\Service\EventService');

How can I get $eventService in a Zend\Form\Form in the same way?

  • 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-05T19:31:25+00:00Added an answer on June 5, 2026 at 7:31 pm

    You have two options if you have a dependency like this. In your case, a Form depends on a Service. The first option is to inject dependencies:

    class Form
    {
      protected $service;
      public function setService(Service $service)
      {
        $this->service = $service;
      }
    }
    
    $form = new Form;
    $form->setService($service);
    

    In this case, the $form is unaware of the location of $service and generally accepted as a good idea. To make sure you don’t need to set up all the dependencies yourself each time you need a Form, you can use the service manager to create a factory.

    One way (there are more) to create a factory is to add a getServiceConfiguration() method to your module class and use a closure to instantiate a Form object. This is an example to inject a Service into a Form:

    public function getServiceConfiguration()
    {
        return array(
            'factories' => array(
                'Event\Form\Event' => function ($sm) {
                    $service = $sm->get('Event\Service\EventService');
                    $form    = new Form;
                    $form->setService($service);
    
                    return $form;
                }
            )
        );
    }
    

    Then you simply get the Form from your service manager. For example, in your controller:

    $form = $this->getServiceLocator()->get('Event\Form\Event');
    

    A second option is to pull dependencies. Though it is not recommended for classes like forms, you can inject a service manager so the form can pull dependencies itself:

    class Form
    {
        protected $sm;
    
        public function setServiceManager(ServiceManager $sm)
        {
            $this->sm = $sm;
        }
    
        /**
         * This returns the Service you depend on
         *
         * @return Service
         */
        public function getService ()
        {
            return $this->sm->get('Event\Service\EventService');
        }
    }
    

    However, this second option couples your code with unnecessary couplings and it makes it very hard to test your code. So please use dependency injection instead of pulling dependencies yourself. There are only a handful of cases where you might want to pull dependencies yourself 🙂

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

Sidebar

Related Questions

In this question , I mentioned my assumption that rubyforge gems are more official,
To keep things simple, please allow the assumption that some code requires the use
How is hashCode() implemented? My assumption is that it uses the object memory location
How would you dynamically subscribe to a C# event so that given a Object
I have some code that prints a document using the System.Drawing.Printing namespace. This now
I was under the assumption that an anonymous user cannot send a presence stanza
I have been under the assumption for a while that viewDidUnload is always called
Possible Duplicate: What is your longest-held programming assumption that turned out to be incorrect?
Did some googling and couldn't find a clear answer on this. My assumption is
std::map<std::string, int> m; // Can I make assumption that m[NoSuchKey] will return 0? std::cout

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.