7

I'm developing an EE module and I want to follow OOP best practices and fulfil the MVC architectural pattern, I have a few questions about how to do this:

  • Where should I define a PHP Class for business logic inside a Module?
  • What are some examples of existing add-ons that are written in a fully OOP style?

1 Answer 1

10

If you're looking to stick with CodeIgniter's pattern for OOP (which is very standard) you would have your data methods in one or more models, your view files (for a control panel interface) in a views directory and your two module files (mcp.* and mod.*) would act as your two controllers.

As such, your directory structure might look something like this:

/mcp.addon.php (controller/logic)
/mod.addon.php (controller/logic)
/models/
/models/addon_model.php (data/model)
/views/
/views/index.php (a control panel view file)
/views/settings.php (view file)

When an add-on is large or needs some portable logic we tend to write libraries/classes outside of the mod or mcp files. You don't have to put everything in those controller files if you see a need to do otherwise.

As for an example, you can look at one of our add-ons, Dev Docs, to see code layout. Each add-on "type" (extension, module, etc) is a controller of its own. Then subdirectories contain other code as expected.

https://github.com/focuslabllc/dev_docs.ee_addon

4
  • Yes, I want portable logic, so libraries/classes. Commented Dec 4, 2012 at 12:52
  • @FilippoSalza in that case you have two options. 1) Create a "CodeIgniter style" library and load it in your module with CI's loader or 2) Write your class to suite your needs across the other platforms you need it portable for, then just use PHP's include() and instantiate the library/class in your Module as needed. Does that help? Commented Dec 4, 2012 at 12:57
  • I create "Libraries" a lot, which are now becoming PSR-2 compliance so I can write code that is framework agnostic. Erik's suggestions are spot-on and pretty much the exact same thing I do. I am increasingly creating new object vs. utilizing the CI singleton to get even more OOP in the mix. Commented Dec 4, 2012 at 15:22
  • Hi Justin! Yeah i prefer create new objects. You are confirm my approach. I come from Java and I feel CodeIgniter a bit 'tight, I want to design my objects. Commented Dec 4, 2012 at 20:59

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.