I need to know what is the purpose of using service container's tagging and how to use it by example this is what I have tried so far.
class MemoryReport
{
}
class SpeedReport
{
}
class ReportAggregator
{
public function __construct(MemoryReport $memory, SpeedReport $speed)
{
}
}
App::bind('MemoryReport', function () {
return new MemoryReport;
});
App::bind('SpeedReport', function () {
return new SpeedReport;
});
App::tag(['MemoryReport', 'SpeedReport'], 'reports');
App::bind('ReportAggregator', function ($app) {
return new ReportAggregator($app->tagged('reports'));
});
$reportAggregator = resolve('ReportAggregator');
dd($reportAggregator);
This is the error I get.
Argument 1 passed to ReportAggregator::__construct() must be an instance of MemoryReport, instance of Illuminate\Container\RewindableGenerator given, called in /media/mazzam/9068A9DC68A9C0F81/M.azzam/Learning/laravel/00 Tutorial/tut/routes/web.php on line 80
Tagging allows you to group services under a common name. This is for example useful if you have multiple services implementing the same interface and you need one of the interfaces method to be executed for each of the implementations:
Note: This is a fictional test case and the underlying services may be different. You also need to add namespaces and
useimports.In your case, you don't need to bind any of the classes. If their construction is based on other services of the service container, type-hinting is sufficient.