您如何在不使用框架的情况下编写良好的PHP代码? [英] How do you write good PHP code without the use of a framework?

查看:141
本文介绍了您如何在不使用框架的情况下编写良好的PHP代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了标准的OO概念之外,还有哪些其他策略可以在不使用框架的情况下生成良好,干净的PHP代码?

Other than standard OO concepts, what are some other strategies that allow for producing good, clean PHP code when a framework is not being used?

推荐答案

请记住:MVC,OOP和层是设计概念,不是语言构造,也不是文件结构.

Remember: MVC, OOP and tiers are design concepts, not language constructs, nor file-structuring.

对我来说,这意味着当不使用框架,并且没有不同的团队进行编程和设计时;在PHP(这是一种模板语言)之上使用另一个模板系统没有任何价值.另外,将代码与布局分开并不一定意味着要在不同的文件上进行

For me, this means that when not using a framework, and when there's not different teams for programming and designing; there's no value in using another template system on top of PHP (which is a template language). Also, separating code from layout doesn't necessarily mean doing it on different files.

这是我过去经常做过的,很少扩展的PHP Web应用程序的方式:

This is how i used to do for one-off, seldom expanded, PHP web apps:

  1. 写一个通用实用程序"文件,在其中放置一些格式化/清理功能以及一些数据库访问功能:
  1. write a 'general utilities' file, there i put some formatting/sanitising functions, as well as a few DB access functions:
  1. getquery():给定一个SQL,返回结果对象
    • getrecord():给出一个SQL,返回一个记录对象(并关闭查询)
    • getdatum():使用SQL,返回一个字段(并关闭查询)
    • 将所有配置(数据库访问,某些URL前缀等)放在"config.php"文件中
    • 编写一个模型层,可以是一个文件,也可以为您存储在数据库中的每个对象编写一个模型层.在那里,将有所有SQL常量,根据您的概念对象而不是数据库记录提供更高级别的API.
  1. getquery(): given a SQL, returns a result object
    • getrecord(): given a SQL, returns a record object (and closes the query)
    • getdatum(): given a SQL, returns a single field (and closes the query)
    • put all configurations (DB access, some URL prefixes, etc) on a 'config.php' file
    • write a model layer, either one file, or one for each object you store on DB. There, will be all the SQL constants, present a higher-level API, based on your conceptual objects, not on DB records.

这是您的框架",然后编写表示"层:

that's your 'framework', then you write the 'presentation' layer:

  1. 每页一个PHP文件,从一些简单代码开始以获取所需的对象,然后是带有经过检查的PHP代码的HTML,只是为了填补漏洞".除极少数例外外,最复杂的代码应为for循环.我制定了仅使用单线的规则,?>应该与开头<?php

  1. one PHP file for each page, starts with some simple code to fetch the objects needed, followed by HTML with interspeced PHP code, just to 'fill in the holes'. with very few exceptions, the most complex code there should be for loops. I make a rule to use only one-liners, the ?> should be in the same line as the opening <?php

  • 每个数据输入表单都应指向一个没有任何HTML的小型PHP,只需获取POST数据,然后输入DB,然后转发到调用页面即可.

就是这样.如果单独工作,它可以将您需要的所有意图分开,而不会因单个用户操作而淹没大量文件.用户看到的每个页面都由一个PHP文件管理.

and that's it. If working alone, it has all the separation of intents you need, without drowning in a lot of files for a single user action. Each page as seen by the user is managed by a single PHP file.

几个月后无需查看代码,它甚至很容易维护,因为它很容易测试应用程序,只需在浏览器的URL字段中记下文件名即可.这样可以直接将您引导至相关代码.

It's even easy to maintain, after a few months without looking at the code, since it's easy to test the app, taking note of the filenames in the URL field of the browser. This guides you directly to the relevant code.

(当然,今天,我正在使用Django来处理几乎所有内容...)

(nowadays, of course, i'm using Django for almost everything...)

这篇关于您如何在不使用框架的情况下编写良好的PHP代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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