I’m working on a package where classes (‘A’, ‘B’ and ‘C’) are extending a "root" class ‘Foo’.
class Foo {
public function abc(){
//
}
}
class A extends Foo {
...
$this->abc();
...
}
class B extends Foo {
...
$this->abc();
...
}
class C extends Foo {
...
$this->abc();
...
}
In a project implementing this package, it needs to overwrite the method ‘abc’ from the class ‘Foo’.
What would be the proper PHP/Laravel way of doing it?
Is there a way to extend/overwrite a package class like a view can be using the vendor folder? That would be an interesting alternative if possible.
Update
To put in perspective the complexity of the situation. Class ‘A’, ‘B’ & ‘C’ are called in several locations within the package. I cannot just extend class ‘A’ in the application as I would need to redefine the namespace path to all the calls made to the class A within the package.
I am considering creating a facade for class ‘A’, ‘B’ & ‘C’ in the package. That way I would be able to overwrite the facade path in the package AppServiceProvider. But I find this approach a bit "dirty". Thus the reason to seek advise for a cleaner implementation.
2
Answers
Found a workaround.
The need to extend the method
abc
was to set thefrom
property within the classFoo
(a Mailable object), based on specific parameters of the application. I need to set this property for all the mail sent.Since I am using a helpers function
bar
within the package to call the various classes (A, B & C).The function in package Helpers file before the workaround
I created a "hook" by adding a call to an optional function within the function
bar
. If declared within the application integrating the package, it will allow to perform the desired operations on the classFoo
.In the application helpers file
Hope it helps someone in a similar position.
Update
For this particular case where I need to alter the from
name
andemail
of all emails being sent, there is an even easier solution.Thanks to this Laracast post.
All I have to do is overwrite the config values in the AppServiceProvider.