有模块支持的PHP应用程序有哪些好的写法? [英] What are some good ways to write PHP application with modules support?

查看:73
本文介绍了有模块支持的PHP应用程序有哪些好的写法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始与我的一个朋友用php编写应用程序,并且想知道您是否对如何在我们的应用程序中实现模块支持有任何建议.

I'm starting to write a application in php with one of my friends and was wondering, if you have any advice on how to implement module support into our application.

或者有没有一种方法可以自动加载由php应用程序用php编写的模块?还是我应该仅依靠__autoload函数?

Or is there a way how to automatically load modules written in php by a php application? Or should i just rely on __autoload function?

并且我们不需要可通过Web界面安装的插件,我们只需要一些聪明的方法就可以将项目的网页与某些类相关联(它们将呈现该页面),因此index.php可以调用正确的类并检索其生成的子页面.

And we don't need plugins that are installable via a web interface, we just need some clever way to associate web pages of our project to some classes (they will render the page) so index.php can call the right class and retrieve it's generated sub-page.

至少到目前为止,我们没有使用任何框架.

And we are not using any kind of framework, for now at least.

推荐答案

发表您的评论:在这种情况下,IMO与严格的文件命名一起自动加载就足够了.

Re your comment: Autoloading in conjunction with strict file naming would be totally enough in this case IMO.

您可以定义一个特定的名称空间(不是以新的PHP 5.3 namespace方式,只是在名称的通用前缀的意义上),例如Module_*获取模块类名称.

You could define a specific namespace (not in the new PHP 5.3 namespace way, just in the sense of a common prefix to names), e.g. Module_* for module class names.

然后,您可以在包含类文件的目录中组织模块,这些类文件仅包含一个类定义,例如:

You could then organize your modules in directories with class files that contain exactly one class definition, for example:

/modules/Mail/index.php   // defines class Module_Mail
/modules/Database/index.php   // defines class Module_Database
/modules/Image/index.php   // defines class Module_Image

只要请求Module_*类,您的自动加载器功能便会出现:

Your autoloader function would then, whenever a Module_* class is requested:

$Database = new Module_Database("localhost", .....);

从正确的目录中包含正确的文件.

include the correct file from the right directory.

就是这样的Zend Framework做到了,而且做得很好.

That's the way e.g. the Zend Framework does it, and does so pretty well.

请考虑选择比Module_更具体的名称空间,以确保将来与其他脚本和应用程序的互操作性.

Consider choosing a more specific namespace than Module_ to assure interoperability with other scripts and applications if that is an option in the future.

略有关联:在一个PHP项目中,您如何组织和访问您的帮助对象?

这篇关于有模块支持的PHP应用程序有哪些好的写法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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