沃梦达 / 编程问答 / php问题 / 正文

PHP 中的完全面向对象的框架

Fully Object Oriented framework in PHP(PHP 中的完全面向对象的框架)

本文介绍了PHP 中的完全面向对象的框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 PHP 中创建一个 100% 面向对象的框架,完全没有过程编程,并且一切都是对象.很像 Java,只是它将在 PHP 中完成.

I want to create a 100% object oriented framework in PHP with no procedural programming at all, and where everything is an object. Much like Java except it will be done in PHP.

任何关于这个东西应该有什么特性的指针,它是否应该使用任何现有的设计模式,比如 MVC?如何为数据库中的每个表创建对象,以及如何显示 HTML 模板等?

Any pointers at what features this thing should have, should it use any of the existing design patterns such as MVC? How creating objects for every table in the database would be possible, and how displaying of HTML templates etc would be done?

请不要链接到现有框架,因为我想自己做这件事,主要是作为学习练习.你因为链接到现有框架作为你的答案并说这就是你想要的"而被否决.

Please don't link to an existing framework because I want to do this on my own mainly as a learning excercise. You will be downvoted for linking to an existing framework as your answer and saying 'this does what you want'.

我希望拥有的一些功能是:

Some features I'd like to have are:

  • 很容易生成 CRUD 页面
  • 基于 AJAX 的分页
  • 如果可能的话,基于 Ajax 的表单验证,或者非常简单的表单验证
  • 可排序的表格
  • 能够使用 PHP 编辑 HTML 模板

推荐答案

我已经经历了你列表中的许多问题,所以让我来说明一下我是如何处理它的.我也是 OOP 上瘾者,并且发现对象技术非常灵活、强大且优雅(如果操作正确的话).

I've gone through many of problems on your list, so let me spec out how I handle it. I am also OOP addict and find object techniques extremely flexible and powerful yet elegant (if done correctly).

MVC - 是的,毫无疑问,MVC 是 Web 应用程序的标准.这是一个有据可查且易于理解的模型.此外,它在应用程序级别上的作用与 OOP 在类级别上的作用相同,也就是说,它使事物分离.MVC 的一个很好的补充是 拦截过滤器 模式.它有助于为预处理和后处理请求和响应附加过滤器.常见用途是记录请求、基准测试、访问检查、缓存等.

MVC - yes, hands down, MVC is a standard for web applications. It is well documented and understandable model. Furthermore, it does on application level what OOP does on class level, that is, it keeps things separated. Nice addition to MVC is Intercepting Filter pattern. It helps to attach filters for pre- and post-processing request and response. Common use is logging requests, benchmarking, access checking, caching, etc.

OOP 表示数据库表/行也是可能的.我使用 DAO 或 ActiveRecord 每天.解决 ORM 问题的另一种方法是 行数据网关 和 表数据网关.这是 示例实现TDG利用ArrayAccess接口.

OOP representation of database tables/rows is also possible. I use DAO or ActiveRecord on daily basis. Another approach to ORM issues is Row Data Gateway and Table Data Gateway. Here's example implementation of TDG utilising ArrayAccess interface.

HTML 模板 也可以表示为对象.我将 View 对象与 Smarty 模板引擎结合使用.我发现这种技术非常灵活、快速且易于使用.表示视图的对象应实现 __set 方法,以便将每个属性传播到 Smarty 模板中.此外,应该实现 __toString 方法以支持视图嵌套.见例子:

HTML templates also can be represented as objects. I use View objects in conjunction with Smarty template engine. I find this technique EXTREMELY flexible, quick, and easy to use. Object representing view should implement __set method so every property gets propagated into Smarty template. Additionally __toString method should be implemented to support views nesting. See example:

$s = new View();
$s->template = 'view/status-bar.tpl';
$s->username = "John Doe";
$page = new View();
$page->template = 'view/page.tpl';
$page->statusBar = $s;
echo $page;

view/status-bar.tpl的内容:

<div id="status-bar"> Hello {$username} </div>

view/page.tpl的内容:

<html>
<head>....</head>
<body>
    <ul id="main-menu">.....</ul>
    {$statusBar}
    ... rest of the page ...
</body>
</html>

这样你只需要echo $page,内部视图(状态栏)就会自动转换成HTML.查看此处的完整实现.顺便说一句,使用拦截过滤器之一,您可以使用 HTML 页脚和页眉包装返回的视图,因此您不必担心从控制器返回完整页面.

This way you only need to echo $page and inner view (status bar) will be automatically transformed into HTML. Look at complete implementation here. By the way, using one of Intercepting Filters you can wrap the returned view with HTML footer and header, so you don't have to worry about returning complete page from your controller.

是否使用 Ajax 的问题在设计时应该不重要.该框架应该足够灵活,能够原生支持 Ajax.

The question of whether to use Ajax or not should not be important at time of design. The framework should be flexible enough to support Ajax natively.

表单验证绝对是可以用OO方式完成的事情.使用 复合模式 构建复杂的验证器对象.复合验证器应该遍历表单字段并分配简单的验证器并给你是/否的答案.它还应该返回错误消息,以便您可以更新表单(通过 Ajax 或页面重新加载).

Form validation is definitely the thing that could be done in OO manner. Build complex validator object using Composite pattern. Composite validator should iterate through form fields and assigned simple validators and give you Yes/No answer. It also should return error messages so you can update the form (via Ajax or page reload).

另一个方便的元素是自动翻译类,用于更改数据库中的数据以适应用户界面.例如,如果您在 db 中有 INT(1) 字段表示布尔状态并在 HTML 中使用导致空字符串或 "on" 在 _POST 或 _GET 数组中的复选框,则您不能只将一个分配给另一个.拥有将数据更改为适用于 View 或 db 的翻译服务是一种清理数据的干净方式.此外,即使在非常复杂的转换过程中,翻译类的复杂性也不会乱扔您的控制器代码(例如 将 Wiki 语法 转换为 HTML).

Another handy element is automatic translation class for changing data in db to be suitable for user interface. For example, if you have INT(1) field in db representing boolean state and use checkbox in HTML that results in empty string or "on" in _POST or _GET array you cannot just assign one into another. Having translation service that alters the data to be suitable for View or for db is a clean way of sanitizing data. Also, complexity of translation class does not litter your controller code even during very complex transformations (like the one converting Wiki syntax into HTML).

i18n 问题也可以使用面向对象的技术来解决.我喜欢使用 __ 函数(双下划线)来获取本地化消息.该函数不是执行查找和返回消息,而是给了我一个 Proxy 对象并预注册消息供以后查找.一旦 Proxy 对象被推入 View 并且 View 被转换为 HTML,i18n 后端会查找所有预先注册的消息.这样,只运行一个返回所有请求消息的查询.

Also i18n problems can be solved using object oriented techniques. I like using __ function (double underscore) to get localised messages. The function instead of performing a lookup and returning message gives me a Proxy object and pre-registers message for later lookup. Once Proxy object is pushed into View AND View is being converted into HTML, i18n backend does look up for all pre-registered messages. This way only one query is run that returns all requested messages.

访问控制问题可以使用业务委托模式来解决.我在我的其他 Stackoverflow 答案中描述了它.

Access controll issues can be addressed using Business Delegate pattern. I described it in my other Stackoverflow answer.

最后,如果您想使用完全面向对象的现有代码,请查看 Tigermouse 框架.页面上有一些 UML 图,可以帮助您了解事物的工作原理.请随时接手这个项目的进一步开发,因为我没有更多的时间来处理它.

Finally, if you would like to play with existing code that is fully object oriented, take look at Tigermouse framework. There are some UML diagrams on the page that may help you understand how things work. Please feel free to take over further development of this project, as I have no more time to work on it.

祝你愉快!

这篇关于PHP 中的完全面向对象的框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:PHP 中的完全面向对象的框架

基础教程推荐