简体   繁体   English

PHP最佳设计实践

[英]PHP best design practices

Ok, have a bunch of questions that I have been thinking about the past few days. 好的,有一堆我过去几天一直在思考的问题。 Currently I have a site that is just a bunch of PHP files with MySQL statements mixed in with PHP, HTML and CSS, basically a huge mess. 目前我的网站只是一堆PHP文件,其中MySQL语句与PHP,HTML和CSS混合在一起,基本上是一团糟。 I have been tasked with cleaning up the site and have made for myself, the following requirements: 我的任务是清理网站,并为自己做了以下要求:

  • The site needs to be efficient and well laid out (the source code), I would like to be able to write as little code as possible. 该站点需要高效且布局合理(源代码),我希望能够尽可能少地编写代码。
  • There has to be good separation between structure, presentation and logic. 结构,表现和逻辑之间必须有很好的分离。
  • For whatever reason, I can't use a framework and need to keep the code maintainable and "simple" as there will be future developers working with it. 无论出于何种原因,我都不能使用框架,需要保持代码的可维护性和“简单性”,因为未来的开发人员将使用它。
  • There needs to be an admin section for at least a few pages. 至少需要几页的管理部分。

Saying that, this is what I know about the site as it is now: 这就是说,这就是我现在对网站的了解:

  • Consists of 10-12 pages, a few are completely static, most are dynamically driven via a database and there is a huge form for users to fill out (20-30 fields) that need to be validated and checked. 由10-12页组成,其中一些是完全静态的,大多数是通过数据库动态驱动的,并且有一个庞大的表单供用户填写(20-30个字段),需要进行验证和检查。
  • The hierarchy of the site is basically 5-6 main pages and then sub-pages within those. 该网站的层次结构基本上是5-6个主页,然后是其中的子页面。

So, knowing those things I wanted to know if anyone had any tips/suggestions as to how to go about doing this with the least amount of headaches. 所以,知道那些我想知道的事情,如果有人有任何提示/建议,如何以最少的头痛去做这件事。

  • Would an OO approach be best in this situation? 在这种情况下,OO方法最好吗?
  • Since there are many static pages and the dynamic pages just need the content filled in would it be best to use some kind of basic template? 由于有很多静态页面,动态页面只需要填写的内容,最好使用某种基本模板吗?

EDIT: Thanks for the answers, when I said no frameworks I basically meant anything that would require new syntax other than PHP, as whoever gets hired to work on this site after me will probably only know PHP. 编辑:谢谢你的答案,当我说没有框架我基本上意味着任何需要除PHP以外的新语法的东西,因为在我之后被雇用在这个网站上工作的人可能只会知道PHP。

Here's an article about how to organize your PHP project, from Rasmus Lerdorf, the architect who created the language: 这是一篇关于如何组织PHP项目的文章,来自创建该语言的架构师Rasmus Lerdorf:

http://toys.lerdorf.com/archives/38-The-no-framework-PHP-MVC-framework.html http://toys.lerdorf.com/archives/38-The-no-framework-PHP-MVC-framework.html

Despite the popularity of OO frameworks for PHP, Rasmus advocates a less OO approach. 尽管面向PHP的OO框架很受欢迎,但Rasmus提倡采用较少的面向对象方法。 He knows more than anyone about PHP intended usage, and how to take advantage of its architecture for high-performance websites. 他比任何人都更了解PHP的用途,以及如何利用其架构来实现高性能网站。

edit: In response to the comment by @theman, I'll concede the article isn't a fine work of writing, but I think the content is important. 编辑:在回应@theman的评论时,我会承认这篇文章不是一篇很好的写作,但我认为内容很重要。 Using PHP as it was intended to be used is better than struggling against its weaknesses to make it fit an OO mold. 使用PHP本打算使用它比努力克服它的弱点以使其适合OO模具更好。

I highly recommend the Smarty templating engine for all PHP projects. 我强烈推荐所有PHP项目的Smarty模板引擎。 It gives you an easy way to separate the logic from the presentation. 它为您提供了一种将逻辑与演示文稿分开的简便方法。

Have a look at this SO question and the answer. 看看这个问题和答案。 It's a pretty good, simple MVC design with some tips on how it can be improved. 这是一个非常好的,简单的MVC设计,提供了一些如何改进它的技巧。 If you are concerned about maintenance, then at the very least you need to seperate presentation from logic (you need a view and controller). 如果您担心维护,那么至少您需要从逻辑中分离演示文稿(您需要一个视图和控制器)。 Smarty forces that, but it is a type of framework and you'll have additional syntax to learn. Smarty强制说,但它是一种框架,你将有额外的语法来学习。

Before you jump on Rasmus' "no framework php mvc framework" bandwagon, read some of the critical comments. 在你跳过Rasmus的“无框架php mvc框架”之前,请阅读一些批评意见。 Any web application structure is a framework, and Rasmus' approach isn't the best I've seen. 任何Web应用程序结构都是一个框架,而Rasmus的方法并不是我见过的最好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM