I am changing my controllers and helper classes to use dependency injection, and it looks like I have helper classes stuck in an infinite loop.
Below is my custom ServiceProvider and two examples of helper classes. As you can see, they introduce each other, so they continue to move forward and backward.
What is the solution to this problem? What error do I seem to be making? . What can I do so that I can run tests on helper classes like General and Person while taunting helper classes called internally
One way that I think might work is through my ServiceProvider, follow these steps:
if (isset($appmade->General)) { // inject the General app that already instantiated } else { $abc = app::make('\Lib\MyOrg\General'); $appmade->General = $abc; }
Is it correct?
// /app/providers/myorg/MyOrgServiceProvider.php namespace MyOrg\ServiceProvider; use Illuminate\Support\ServiceProvider; class MyOrgServiceProvider extends ServiceProvider { public function register() { $this->app->bind('\Lib\MyOrg\General', function ($app) { return new \Lib\MyOrg\General( $app->make('\Lib\MyOrg\Person'), $app->make('\App\Models\User') ); }); $this->app->bind('\Lib\MyOrg\Person', function ($app) { return new \Lib\MyOrg\Person( $app->make('\Lib\MyOrg\General'), $app->make('\App\Models\Device') ); }); } } // /app/libraries/myorg/general.php namespace Lib\MyOrg; use App\Models\User; use Lib\MyOrg\Person; class General { protected $model; protected $class; public function __construct(Person $personclass, User $user) { } } // /app/libraries/myorg/person.php namespace Lib\MyOrg; use App\Models\Device; use Lib\MyOrg\General; class Person { protected $model; protected $class; public function __construct(General $generalclass, Device $device) { } }
source share