I laravel 9 app I use EmailingServiceProvider with conditional binding of EmailingService depending on configuration :
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class EmailingServiceProvider extends ServiceProvider
{
public function register()
{
$emailingSupport = config('app.emailing_support');
if($emailingSupport === 'MailerLiteApi') {
$this->app->bind('App\Library\Services\EmailingServiceInterface', 'App\Library\Services\EmailingService');
}
}
}
class EmailingService is called in controller ok, except cases when I want to comment binding code above : // $this->app->bind
I tried to use example at https://www.php.net/manual/en/reflectionclass.isinstantiable.php as :
...
use App\Library\Services\EmailingServiceInterface;
class AuthorController extends Controller
{
private AuthorMethodsServiceInterface $authorMethodsService;
private EmailingServiceInterface $emailingService;
public function __construct(AuthorMethodsServiceInterface $authorMethodsService /*, EmailingServiceInterface $emailingService*/ )
{
parent::__construct();
$this->authorMethodsService = $authorMethodsService;
// $this->emailingService = $emailingService; // I had to comment this and declarations above
}
// I expected that $reflectionClass->isInstantiable() would have false value below
$reflectionClass = new \ReflectionClass('EmailingService'); // Line points at this line
if $reflectionClass->isInstantiable()) {
$this->emailingService = new EmailingService();
$assignedAuthor = $this->emailingService->method();
But I ggot error":
"message": "Class \"EmailingService\" does not exist",
"exception": "ReflectionException",
How that can be done ?
MODIFIED BLOCK :
That does not work. With lines :
use App\Library\Services\EmailingServiceInterface;
...
$yourService=new EmailingService(); // Error pointing at this line
if ($author instanceof \Illuminate\Http\JsonResponse and $yourService instanceof EmailingServiceInterface) {
...
I got error:
"message": "Class \"App\\Http\\Controllers\\EmailingService\" not found",
"exception": "Error",
?
Thanks!