当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Separating application from app library, shared library, components, etc

问题描述:

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?

网友答案:

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: