how get static method of service

Notice: This thread is very old.
leonardoap
Member | 17
+
0
-

hi there, how can i get static method of presenter by example:

my config.neon

services:
    models: App\Common\Classes\ServiceFactory::serviceModels

and when i try to inject into my presenter with

	/**  @inject @var \App\Common\Classes\ServiceFactory::serviceModels */
	public $models;

the browser show me the next error

Nette\InvalidStateException

Class or interface 'App\Common\Classes\ServiceFactory::serviceModels' used in @var annotation at App\Common\Presenters\BasePresenter::$models not found. Check annotation and 'use' statements
Filip Procházka
Moderator | 4668
+
0
-

You can't actually. You can configure the DI to push somewhere result of some method call using neon configuration. But you can't require result of some method call.

services:
	-
		class: App\ArticlePresenter()
		setup:
			- $models(App\Common\Classes\ServiceFactory::serviceModels())

This will configure the service to contain the result of the method call in the property, once it's created by the DI Container.


And why is it static? Please don't try to create any new model/service locators inside the container. Simply register all the services you're going to be using and then explicitly require them in your class.

Like this

services:
	- App\Articles\ArticleFacade()

and get it injected using property injection in presenters

class ArticlePresenter extends BasePresenter
{

	/** @var \App\Articles\ArticleFacade @inject */
	public $articles;

	// ...

or constructor injection in bussines logic classes

class MyOtherModelThatRequiresWorkingWithArticles
{
	private $articles;

	public function __construct(\App\Articles\ArticleFacade $articles)
	{
		$this->articles = $articles;
	}

	// ...

Last edited by Filip Procházka (2015-02-05 18:45)