2

I am working on an application that will have a "library" folder which will contain a homemade framework/library (session, DB, cache,config type classes) and then a "modules" folder which will contain folders for the sections of my app (ie; blogs/, forums/, account/, etc,etc)

In most of my modules (blogs, forums, etc), I will need multiple objects like cache, database, logger, config objects. I was planning on using dependency injection for this but I am curious, couldn't I just have a Core class/object that could do stuff like my database, cache, logger, time, methods and then just extend this core class into my other module classes and have access to all these things without needing to inject them?

I am pretty new to using classes/object so I may be way off here, please explain.

4 Answers 4

9

Classes should have a single responsibility. A Core class doing caching, db access, logging and time, etc is effectively a God Object aka The Blob. It's an AntiPattern. Don't do that. Make them SOLID.

Sign up to request clarification or add additional context in comments.

Comments

5

Extending classes makes sense if you specialize classes or, well, extend them.
It doesn't make sense and usually becomes messy if the extension has nothing to do with the original purpose of the base class.

For example, you may extend a DbBase class with a DbMySql class (specialization), or an HtmlHelper with an Html5Helper (extension).

You should not extend your DbClass into a ProductsModule. Both have nothing to do with each other. The ProductsModule is not inherently bound to a database, so you're mixing responsibilities that have nothing to do with each other.

Comments

1

I also agree with Gordon.

Personally I avoid the Factory static method approach. In essence, it's equivalent to using a global variable. It's the Service Locator anti-pattern.

In addition to the suggested DI container, I find manual dependency injection (i.e. with using a DI container) to be a valuable approach. There's no tool (i.e. container) that can design your classes to be loosely coupled, and I find that using manual DI is helpful for focusing on that.

[P.S. I would have added this as a comment, but I'm new and am unable to comment]

Comments

0

What you CAN do, is creating a Factory class that instantiates and provides the single objects you need. Somthing like:

$db = myFactory::getDBO();
$conf = myFactory::getConfig();
$session = myFactory::getSession();

and so on.

2 Comments

A DI Container would be better suited, f.e. components.symfony-project.org/dependency-injection
@Gordon this is what I mean. In Joomla's Framework it's the Factory class that provides this services... it's even called JFactory :)

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.