1

So the solution was to create a new ServiceProvider.

This solution works for Override

php artisan make:provider MyServiceProvider

Which extended the Vendor service provider (found within config/app.php). Within that ServiceProvider, add my alias within the overridden register method

 $loader->alias('Vendor\VendorName\Class', 'App\Vendor\MyCustomClass');

https://stackoverflow.com/a/47926486/10589868

Now, how do I extend the overridden class? I tried this:

$loader->alias('ClassParent', 'Vendor\VendorName\Class');
$loader->alias('Vendor\VendorName\Class', 'App\Vendor\MyCustomClass');
...
class MyCustomClass extends ClassParent {} // not working
Oladipo
  • 1,579
  • 3
  • 17
  • 33
Jan Grb
  • 21
  • 1
  • 5

1 Answers1

2

First thing you need to do is extend the Vendor class:

class MyCustomClass extends Vendor\VendorName\Class {}

Now, this class has the properties and methods of the Vendor class and the properties and methods you've added.

Then, your custom class can become an alias:

 $loader->alias('App\Vendor\MyCustomClass', 'Vendor\VendorName\Class');
Oladipo
  • 1,579
  • 3
  • 17
  • 33
  • Not working, maybe i wasn't clear enough... Now it's only extends older class. I want to override it, not to use it directly. This class is been used in other vendor class, and i want to use my overridden and extended class. exp. VendorClass1 {} VendorClass2 { using VendorClass1; } MyClass extend VendorClass1 {} ---- MyClass is alias for VendorClass1 VendorClass2 is using MyClass, not VendorClass1 – Jan Grb Nov 06 '18 at 08:24
  • @JanGrb Oh, I see. I think this will be helpful for you: https://stackoverflow.com/questions/31435747/laravel-extend-a-vendor-class-installed-from-composer. This also gives some outside hints: https://stackoverflow.com/questions/22884764/laravel-extend-form-class – Oladipo Nov 07 '18 at 11:45
  • 2
    I think this is exactly what I want. Where does `$loader->alias` go? – brandonbanks Nov 09 '19 at 15:18