I am using Laravel 8. I want to know the difference between new User()
and User::class
because I am having trouble while using new User()
.
Here is the scenario,
I have UserServices
class in which I am injecting UserRepository
class
class UserServices
{
private $userRepository;
public function __construct(UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}
public function findUserByEmail($email){
return $this->userRepository->findUserByEmail($email);
}
}
Now in service provider when I bind the UserServices class using following way I am getting following error
ArgumentCountError: Too few arguments to function
$this->app->bind(UserServices::class, function($app){
return new UserServices();
});
But when I use UserServices::class
then it works fine
$this->app->bind(UserServices::class);
Why?
I know that UserServices
class constructor is expecting parameter then why working with UserServices::class
//Working
$this->app->bind(UserServices::class);
//Not Working
$this->app->bind(UserServices::class, function($app){
return new UserServices();
});