6

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.

3 Answers 3

13

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

{
    "name": "your/custom-date",
    "autoload": {
      "files": ["src/lib/Zend/Date.php"]
    }
}

src/lib/Zend/Date.php

class Zend_Date
{
    ...
}

Edit: After you edited a composer.json file, run composer dump-autoload to update the autoloader.

10
  • using this method, do we still need to add preference in dl.xml?
    – belfort1
    Commented Sep 14, 2016 at 8:19
  • No, as I said,di.xml cannot be used with classes not instantiated by the Magento object manager. This method loads your Zend_Date class instead of the original one (same class name!), so it is similar to using app/code/local in Magento 1 Commented Sep 14, 2016 at 8:25
  • I want to override this class everywhere in magento, in a way that in no place, magento uses its own Zend_Date class.
    – undone
    Commented Sep 14, 2016 at 16:00
  • Yes, that's what this is doing. Commented Sep 14, 2016 at 17:50
  • 1
    I can't think of any other way, sorry. But Magento 2 always uses the composer autoloader, even if you did not install it using composer. So it should always be possible in some way. Commented Sep 15, 2016 at 19:14
2

From my experience, we shouldn't try to override a Zend class on this way. We should create a new class, this class will extends from Zend_Date. And then, try to override some Zend Date methods in your class as you want.

4
  • I want to change the output of toString method. If the way you're talking about can do this, Please explain how can I do it. Thanks .
    – undone
    Commented Sep 14, 2016 at 13:26
  • how to increase captcha size for forgot password and customer login page programmatically.Any idea do you have related it please tell me. Commented Oct 29, 2018 at 14:55
  • @hiteshbalpande should check here: magento.stackexchange.com/a/141976/33057 Commented Oct 30, 2018 at 6:18
  • while I agree with you, there are reasons you may want to do this. Patching known issues for example. The question is how, not if you should. Commented Apr 12, 2019 at 14:12
0

Seems to me like an easier solution is this

Modify Magento 2 composer.json

"autoload": {
    [...]
    ,
    "files": [
        [...],
        "lib/Zend/Http/Response.php"
    ]
},

Copy the vendor/magento/zendframework1/library/Zend/Http/Response.php to lib/Zend/Http/Response.php

Then run composer dump-autoload

Warning: You are overriding entire class. But this might suit your need.

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.