I have an error class which I have made a Facade and a ServiceProvider for.
I use it like this:
Err::getLastError();
I have also another class for file validation:
FileValidate::IsImage($this->getUpload());
I want to inject the Err facade into the FileValidate
so that I use it like this:
FileValidate::Error()->getLastError();
How should I do this?
Now My approach is that, in FileValidate
class I add a member:
function Error()
{
return $this;
}
Though the above just returns the FileValidate
object, thus I add another method:
function getLastError()
{
return Err::getLastError();
}
But then for each method of Err
, I should make an alternative in FileValidate
and all Err like the above example. I need a more dynamic solution.
In your
FileValidate::Error()
method return the error class rather than an instance ofFileValidate
:This will return your error object which should have whatever methods on it that you need without having to duplicate them on another class for no reason.
Another alternative could be to add the error object into the
FileValidate
's constructor:After updating your file validate's service provider, you could then just return that object from your
Error
method: