You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This works because I'm passing the exact class Plugin_Options as the constructor type hint (I've set the use My_Plugin\Options\Plugin_Options above the class definition).
But, this also tightly couples my code, because I'm depending on concretes not abstracts (interface).
So, since my Plugin_Options class implements the same Service interface as 99.8% of my classes (service interface only has one public method register for registering hooks in WordPress), I tried to type hint against the Service interface
[09-May-2019 13:35:01 UTC] PHP Fatal error: Uncaught DI\Definition\Exception\InvalidDefinition: Entry "My_Plugin\Admin\Admin_View" cannot be resolved: Entry "My_Plugin\Core\Service" cannot be resolved: the class is not instantiable
Full definition:
Object (
class = #NOT INSTANTIABLE# My_Plugin\Core\Service
lazy = false
)
Full definition:
Object (
class = My_Plugin\Admin\Admin_View
lazy = false
__construct(
$plugin_options = get(My_Plugin\Core\Service)
)
) in /public_html/wp-content/plugins/my-plugin/vendor/php-di/php-di/src/Definition/Exception/InvalidDefinition.php:18
Stack trace:
#0 /public_html/wp-content/plugins/my-plugin/vendor/php-di/php-di/src/Definition/Resolver/ObjectCreator.php(155): DI\Definition\Exception\InvalidDefinition::create(Object(DI\Definition\ObjectDefinition), 'Entry "My_Plugi...') #1 /public_html/wp-content/plugins/my-plugin/vend in /public_html/wp-content/plugins/my-plugin/vendor/php-di/php-di/src/Definition/Exception/InvalidDefinition.php on line 18
[09-May-2019 14:30:31 UTC] PHP Fatal error: Uncaught DI\DependencyException: Circular dependency detected while trying to resolve entry 'My_Plugin\Core\Service' in public_html/wp-content/plugins/my-plugin/vendor/php-di/php-di/src/Container.php:371
Stack trace:
#0 public_html/wp-content/plugins/my-plugin/vendor/php-di/php-di/src/Container.php(137): DI\Container->resolveDefinition(Object(DI\Definition\AutowireDefinition)) #1 public_html/wp-content/plugins/my-plugin/vendor/php-di/php-di/src/Definition/Reference.php(53): DI\Container->get('Developer_Porta...') #2 public_html/wp-content/plugins/my-plugin/vendor/php-di/php-di/src/Definition/Resolver/ResolverDispatcher.php(59): DI\Definition\Reference->resolve(Object(DI\Container)) #3 public_html/wp-content/plugins/my-plugin/vendor/php-di/php-di/src/Definition/Resolver/ParameterResolver in public_html/wp-content/plugins/my-plugin/vendor/php-di/php-di/src/Container.php on line 371
Is the problem that I'm using the same interface in most places? Should I create separate interface and then pass this one (even thought that interface may just implement the Service interface)?
Or am I doing something wrong completely? I've read the documentation but had no luck so far :/
Additional info
PHP Version: 7.2.14
php-di Version: 6.0.8
The text was updated successfully, but these errors were encountered:
Your Admin_View implements Service and requires Service as a constructor parameter that is implemented by Admin_View?
How would you try to resolve this manually?
$admin_view = new Admin_View(new Admin_View(new Admin_View(...)));
I'd suggest that you try to write code without PHP-DI that resolves your class injection manually for one case first. That way you'll easily find why you are unintentionally creating circles. As a result you'll be able to create the interfaces and classes properly, and PHP-DI will work then.
Yeah, I've tried to resolve this issue for a couple of hours without luck. I guess I'll have to rewrite the Plugin_Options class so that I can inject it properly in the Admin_View class.
I'm doing a refactor of an old code I wrote and I'm trying to write everything with proper OOP. That's why I'm looking into a DI container, to make my life a bit easier. Both of those classes are not refactored (yet) 😬
I'm trying to use
php-di
in my WordPress plugin. In my plugin class I have tried withIn my main plugin file I'm calling the
(new Plugin())->register()
method via plugin factory to kickstart it all.The
get_service_classes
method returns array of classes that need to be instantiated and autowired. For instance:In one example I have
This works because I'm passing the exact class
Plugin_Options
as the constructor type hint (I've set theuse My_Plugin\Options\Plugin_Options
above the class definition).But, this also tightly couples my code, because I'm depending on concretes not abstracts (interface).
So, since my
Plugin_Options
class implements the sameService
interface as 99.8% of my classes (service interface only has one public methodregister
for registering hooks in WordPress), I tried to type hint against theService
interfaceThis threw an error:
The issue is that
Service
is an interface.I've tried by defining the
ContainerBuilder
But that caused circular definitions error.
Is the problem that I'm using the same interface in most places? Should I create separate interface and then pass this one (even thought that interface may just implement the
Service
interface)?Or am I doing something wrong completely? I've read the documentation but had no luck so far :/
Additional info
PHP Version: 7.2.14
php-di Version: 6.0.8
The text was updated successfully, but these errors were encountered: