0

I am not able to overwrite the reorder.php file which is an abstract class. Anyone can help whether I have use di.xml or any other method.

2 Answers 2

2

As per The architecture followed by Magento, We can not rewrite any Abstract class. Rewriting a class means, instantiate the new class instead of old class. Since an abstract class is never instantiated, it can never be rewritten.

To solve this issue, you can check for the actual class that is extending the abstract class. Rewrite the child class and override any property in your newly created class.

For example, suppose a class named A extends an abstract class B and defined any function/property here in A. you can rewrite the class A with C and override function/property in your class C.

This is how you can achieve your goal.

0
1

By design you can't overwrite an abstract class. Abstract classes are loaded because they are extended from by the classes themselves and not handled through the Magento DI handler.

Overwrite the classes that extend from the abstract class instead.

Because I was curious if my statement above was 100% correct I went and took a look at the code. Near as I can tell Magento makes it impossible to load a file from anywhere besides the defined directories. It looks at generated/code/ first, then vendor/ and then finally app/code/.

If you really want to do this:

in app/bootstrap.php find:

// Sets default autoload mappings, may be overridden in Bootstrap::create
\Magento\Framework\App\Bootstrap::populateAutoloader(BP, []);

Right after it, add this:

// custom autoloader to prepend the regular autoloading for local overrides
spl_autoload_register(function($class) {

    $localCodePool = BP.'/app/code/';
    $file = str_replace('\\', '/', $class);
    $path = $localCodePool.$file.'.php';

    if (file_exists($path)) {
        require_once $path;
    }

}, true, true);

That adds a custom autoloader that gets called before the existing autoloaders and will look for a local version of the file in app/code/. Again, I believe this to be a bad idea. If the bootstrap file is ever updated, you lose the autoloader and you need to keep the local versions of the classes up to date with updates done through composer.

1
  • Hi, Thank you for your replay. I want to know how to overwrite abstract class. One small example
    – karthik
    May 30, 2018 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.