可以选择自定义类别,但可以使用统一的类别名称 [英] Having the option of customized classes but a unified class name

查看:87
本文介绍了可以选择自定义类别,但可以使用统一的类别名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您正在构建一个将成为打包产品的Web应用程序,用户希望该应用程序能够扩展和定制.

Suppose you are building a web application that is going to be a packaged product one day, one that users will want to be able to extend and customize.

它带有一个核心库,该库由包含类的PHP文件组成:

It comes with a core library consisting of PHP files containing classes:

/library/
/library/frontend.class.php
/library/filesystem.class.php
/library/backend.class.php

现在,假设您要保留用户无法修补的干净核心.尽管如此,您仍然希望用户能够根据需要自定义每个螺母和螺栓.

Now, suppose you want to keep a clean core that users can't patch. Still, you want the user to be able to customize every nut and bolt if need be.

我当前的想法是创建一种自动加载机制,当实例化一个类时,该机制首先加载核心包括:

My current idea is to create an autoloading mechanism that, when a class is instantiated, first loads the core include:

/library/frontend.class.php

然后,它切换到user目录,并查看是否包含相同名称的包含项:

then, it switches to the user directory and looks whether there is an include of the same name:

 /user/library/frontend.class.php

如果存在的话,它也包括在内.

if one exists, it includes that as well.

很明显,用户include必须包含一个扩展核心include中的定义的类定义.

Obviously, the user include must contain a class definition that extends the definition in the core include.

现在我的问题是,我将如何实例化此类?毕竟,我始终可以确定以下内容的定义:

Now my question is, how would I instantiate such a class? After all, I can always be sure there is a definition of:

class frontend_core

但我不确定是否有一个

class frontend_user extends frontend_core

但是,我希望能够依赖并实例化一个类名,而不管是否对该类进行了自定义扩展.

However, I would like to be able to rely on, and instantiate, one class name, regardless of whether there was a custom extension to the class or not.

是否有一个聪明的方法,构想或模式来实现这一目标?

Is there a clever way, idea, or pattern how to achieve this?

当然,我可以编写一个简单的工厂帮助程序函数,该函数首先查找user类,然后查找core类并返回初始化的对象,但是我真的想保持它的简洁和简单.可能,因为正如我所说,它将成为包装产品.

Of course, I could write a simple factory helper function that looks for the user class first and then for the core class and returns an initialized object, but I would really like to keep this as clean and simple as possible, because as I said, it is going to be a packaged product.

我正在寻找一种使用最少代码并引入尽可能少的新功能的智能技巧或模式.

I am looking for a smart trick or pattern that uses as little code, and introduces as little new functionality, as possible.

推荐答案

您为什么不遵循Propel所采用的方法?您生成了基类,并且已经提供了一个空的User类(扩展了基类),您的用户可以在其中放置其覆盖/特定的实现详细信息,并且在您的代码中始终引用User类.因此,基本上,您只需要使用所描述逻辑的逆序即可.

Why don't you follow the approach as used by Propel? You generate your base classes and already provide an empty User class (extending the base class) where your users can put their overrides/specific implementation details, and in your code you always refer to the User classes. So basically you just use the inverse of the logic you described.

如果上述解释不清楚,请查看

If the explanation above isn't clear, check out http://propel.phpdb.org/trac/wiki/Users/Documentation/1.4/QuickStart#a6.UsingtheGeneratedSQLandOMFiles and generate code for a small database. The base classes are in the om folder, the (by default empty) user classes are in the root folder.

这篇关于可以选择自定义类别,但可以使用统一的类别名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆