How to enable php class that extends interface

I am developing a socket listener that should process data using the CommandService class.

CommandService.php

<?php namespace Application\Service; use Zend\ServiceManager\ServiceLocatorAwareInterface; use Zend\ServiceManager\ServiceLocatorAwareTrait; class CommandService implements ServiceLocatorAwareInterface { use ServiceLocatorAwareTrait; } 

custom.php

 <?php include('../module/Application/src/Application/Service/CommandService.php'); ?> 

And when I start the console

 php custom.php 

I get the error: Fatal error: the interface "Zend \ ServiceManager \ ServiceLocatorAwareInterface" was not found in C: \ wamp \ www \ nutirent \ module \ Application \ src \ Application \ Service \ CommandService.php on line 7

So, it would be great if someone can give me some advice on how I can run this without making custom.php as a class.

+2
source share
2 answers

If you are not using an automatic boot system (that is, a composer autoloader), then it is your responsibility to download all the dependencies.

In this case, just loading the CommandService is not enough, and you also need to download (enable) Zend \ ServiceManager \ ServiceLocatorAwareInterface , as well as all other dependencies :)

Therefore, I highly recommend considering using an autoloader ;)

+3
source

Ali's answer is completely right, I just want to say something about ServiceLocatorAwareInterface:

Some time ago I was a big fan of ServiceLocatorAwareInterface in my services. Now I'm not sure about that. You must consider this:

Ask ServiceLocator in your Services to make them useless.

Since the service locator is what it is, you can’t talk anymore about what your class depends on, because there’s potentially everything. Instead, you should use Dependancy injection and download everything you need in the Service, with factories, factories - this is the only place where you can use ServiceLocator, this is the place where it belongs.

Ocramius said that it’s better than what I’m trying to say right now, so I link his work here about this:

Best Practices Ocramius Zf2

It inspired me. Hope you too.

+1
source

Source: https://habr.com/ru/post/1235108/


All Articles