views:

57

answers:

1

I am creating an MVC application framework, using only libararies and components that I create myself (mainly a learning experience), but I'm not quite sure how to separate each kind of library from one another.

We'll call my application Cat.

Lets say I'm creating a library called Dog, which would sort of be like Zend and is full of different components that do different tasks (Database classes, DALs, Routers for figuring out what Controllers to choose from a given URL, etc), and will be located in the root/library/ dir.

I'll also be creating an app library that would be application specific, (Might contain a class like FrontController or Application to help with initiating and configuring the app). This will be located in root/app/library/

I want to use the Dog library on both this application and other applications, and for it to be hopefully independant of the Cat, so it can be used on a number of other applications.

In my Cat application, lets say I create a new database object. Should I be writing:

$database = new Dog_Database();

or would it be better to have a Cat_Database class that purely extends the Dog_Database class? That would mean I can later tell the Cat_Database to extend a Ferret_Database instead if needed...

I guess the main question is, should my application be calling things straight from a shared library, or should it be calling app-specific library classes, which in turn extend from the shared library if needed?

+1  A: 

I think you answered your own question somewhat. You say that you want dog to be independent of cat. That means, to me, using a lot of dependency injection between the two and not a lot of inheritance or direct calling. You can't make your app library extend classes from the framework if you want to reuse the app library elsewhere sans the framework.

Its tough to give detailed advice to a broad question like this. Perhaps a few of these concepts will help you though.

Dependency Injection

Programming to an interface

Evord
I took a look into DI and I think I understand it pretty well. Basically the idea is to create an object and pass initiated objects to it, rather than letting the object itself initiate other objects, correct? You said I can't have the app lib extend framework classes because I want to reuse the app somewhere else, but actually the app itself doesn't need to work with other frameworks; I want the Framework to work with other apps. And how about utility classes? Dog_Url::getDomain() etc..? Should framework files (index.php, boostrap.php) use these, as well as controllers/models?
manbeardpig
This doesn't fully answer my question, but it has helped, so I think I'll 'accept' this answer. If anyone else has any answers though, please feel free to post them. (Or answer the questions from my last comment). Thanks Evord!
manbeardpig