4

I am creating some magento module, and I need to use some 3rdParty classes "ThiredPartyClassA" and "ThiredPartyClassB" that are in an external file "thirdPartyCode.php".

where should I place the file thirdPartyCode.php ? and how should I refer (require_once) to if so that I can use it within one of my Action handlers ?

Thanks, Eyal

3 Answers 3

14

I ended up creating a lib directory under my modules main directory. It seemed to be the "best of both worlds".

$ExternalLibPath=Mage::getModuleDir('', 'My_Module') . DS . 'lib' . DS .'EXTERNALLIB.php';
require_once ($ExternalLibPath);
Sign up to request clarification or add additional context in comments.

2 Comments

Problem of that approach is the setup:di:compile command will run EXTERNALLIB.php file during installation and if this file generates any output it will break the compile process and crash the store.
So what's the best solution?
4

You can require files in PHP just as you could without Magento, so you can actually use require_once if need be. If you want to keep your code cleaner, you may want to put it into the /lib folder in Magento since it is a system library. I am not sure if this is in the default include path, so you may have to fiddle with the require.

For cleanliness, you may also want to make a wrapper around this code and use Magento models/helpers to manipulate them.

Hope that helps!

Thanks, Joe

2 Comments

I did not want to put it in the common /lib folder as it felt to me that this will be to exceed my rights as a module developer.
If you don't want it in lib, feel free to drop it inside of your module instead. Still, the rest of the answer stands.
1

As with any PHP script - you're free to include files wherever you feel like.

However you might want to use the built in autoloader in Magento. I'm pretty sure Magento uses Zend for autoloading. (Either way it's available to you)

Zend, unlike Magento, have good documentation. Check it out here http://framework.zend.com/manual/en/zend.loader.autoloader.html

1 Comment

I've tried this before, but you end up having to port the code manually in a lot of cases, and then doing upgrades is a pain. Quarantining the code in one place and using wrappers for it has worked much better for me in the past.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.