I want to override a Zend class (Zend_Date
). In Magento 1, I used to put Date.php
in app/code/community/Zend
and Magento itself used to do the rest. But now, there is no such directory. How can I override that class?
Edit:
I installed Magento_SampleNewPage module and it shows Hello world!
message properly when I go to http://domain.com/newpage
. I edited composer.json
and added my Date.php
to it:
{
"name": "magento/sample-module-newpage",
"description": "A Magento 2 module that creates a new page",
"type": "magento2-module",
"version": "1.0.0",
"license": [
"OSL-3.0",
"AFL-3.0"
],
"require": {
"php": "~5.5.0|~5.6.0|~7.0.0",
"magento/framework": "~100.0"
},
"autoload": {
"files": [ "Zend/Date.php" , "registration.php" ],
"psr-4": {
"Magento\\SampleNewPage\\": ""
}
}
}
But still, it doesn't work. When I instantiate a new Zend_Date
, it's from vendor/Magento/zendframework1/library/Zend/Date.php
not my Zend_Date
.
Best Answer
There is no "local code pool override" mechanism anymore. Instead you can use preferences in
di.xml
for any class that is instantiated by the Magento object manager, i.e. used via Dependency Injection in Magento classes. This is the equivalent to a class rewrite in Magento 1.But for classes like
Zend_Date
where this is not the case, this is not possible.However you could create a composer package that loads your own
Zend_Date
class before the real one is loaded via autoloading:composer.json
src/lib/Zend/Date.php
Edit: After you edited a composer.json file, run
composer dump-autoload
to update the autoloader.