How to pass data to the static method NOT calling it statically?

I have data which is coming, I want to encapsulate them to endure the validation into a separate class.

Controller
public function registerNewUser(Request $request)
{
 CreateUserRequest::validate($request);


Validation
public function __construct(
 UserRepositoryInterface $userRepository
 ) {
 $this->userRepository = $userRepository;
}
 public function rules($request)
{
dump($request);
// do data validation


However, you can't do that, and call via new Class() as it is impossible, because there in the constructor Dependency Injection dependencies

Please tell me how to pass data to the class validator?
June 10th 19 at 16:17
1 answer
June 10th 19 at 16:19
Solution
If the answer to your question, to use a Singleton/Multiton, they can't call static method using a static instance of the class.

But this is not your case, because you have Validation needs to be in some kind of service container and from the controller to be called via it.

An example of how not to do it :)
class Validation {

 public static $instance;

 public function __construct()
{
 self::$instance = $this;
}

 public function rules($request)
{
dump($request);
}
}

class Controller {
 public function registerNewUser(Request $request)
{
Validation::$instance->rules($request);
}
}

Thanks, interesting solution - jaycee commented on June 10th 19 at 16:22

Find more questions by tags PHP