PHP中完全面向对象的框架 [英] Fully Object Oriented framework in PHP

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

问题描述

我想在PHP中创建一个100%面向对象的框架,并且完全不需要过程编程,而且所有东西都是一个对象。就像Java一样,除了它将在PHP中完成。



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

请不要链接到现有的框架,因为我我想主要是作为一种学习练习。你会 被低估,将其链接到一个现有的框架作为你的答案,并说'这是你想要的。'



喜欢的是:


  • 非常容易的CRUD页面生成

  • 基于AJAX的分页

  • 基于Ajax的表单验证,如果可能的话,或者非常简单的表单验证

  • 可排序表格

  • 能够使用PHP编辑HTML模板


解决方案

我列出了许多问题,请让我指出我如何处理它。我也是面向对象的瘾君子,并且发现对象技术非常灵活,功能强大而且非常优雅(如果做得对的话)。


$ b MVC MVC是Web应用程序的标准。它是有据可查的,可以理解的模型。此外,它还在应用程序级别上进行了OOP在课堂级别上的操作,即保持事物分离。对MVC的好处是拦截过滤器模式。它有助于附加过滤器,用于预处理和后处理请求和响应。常见的用途是记录请求,基准测试,访问检查,缓存等。数据库表/行的OOP表示也是可能的。

bb b
我使用 DAO ActiveRecord 。另一种解决ORM问题的方法是行数据网关表格数据网关。以下是示例实施的TDG利用 ArrayAccess 接口。
$ b HTML模板也可以表示为对象。我将View对象与Smarty模板引擎结合使用。我发现这种技术极其灵活,快速且易于使用。表示视图的对象应该实现 __ set 方法,这样每个属性都会传播到Smarty模板中。另外应该实现 __ toString 方法来支持视图嵌套。看例子:

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

view / status-bar.tpl

 < div id =status-bar>您好{$ username}< / div> 

view / page.tpl 的内容:

 < html> 
< head> ....< / head>
< body>
< ul id =main-menu> .....< / ul>
{$ statusBar}
...页面的其余部分...
< / body>
< / html>

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



<在设计时,是否使用Ajax的问题​​不应该很重要。该框架应该足够灵活,可以本地支持Ajax。



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

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

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



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



最后,如果您想要使用完全面向对象的现有代码,请参阅 Tigermouse框架。网页上有一些UML图表,可以帮助您了解事情的工作方式。请随时接管这个项目的进一步开发,因为我没有更多的时间来处理它。



有一个很好的黑客!


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.

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:

  • Very easy CRUD page generation
  • AJAX based pagination
  • Ajax based form validation if possible, or very easy form validation
  • Sortable tables
  • Ability to edit HTML templates using PHP

解决方案

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 - 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 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 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;

Contents of view/status-bar.tpl:

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

Contents of view/page.tpl:

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

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.

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).

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).

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.

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

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.

Have a nice hacking!

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

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