I have a custom module (my_module); Inside this module I have a custom class (MyClass), where I want to use a Drupal service (file_system).
This is my code.
src/MyClass.php
namespace Drupal\my_module;
use Drupal\Core\File\FileSystem;
class MyClass {
public function __construct(FileSystem $file_system) {
}
public static function create(ContainerInterface $container) {
return new static(
$container->get('file_system')
);
}
}
When I instantiate this class (in a controller for example), I get an error.
$my_class = new MyClass();
TypeError: Argument 1 passed to Drupal\my_module\MyClass::__construct() must be an instance of Drupal\Core\File\FileSystem, none given
What is wrong in my code?