What's the best practice and recommended way to extend an object like \Magento\Store\Model\Store with a new object property.

I want to do something like this:

$storeManager; //StoreManagerInterface
foreach($storeManager->getStores() as $store) {
    $language = $store->getLanguage(); // < this should be an own object from my custom module

I think I should use the di config. Does anybody has a good idea?



Magento recommends to use composition instead of inheritance.
If you want to use any object in your class, then you can include that in your class using constructor dependency injection
To inject a non-injectable class in your class, Magento recommends to use factories
Also, Magento recommends to use Public Interfaces and Service Contracts while creating your custom functionality.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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