您使用什么PHP应用程序设计/设计模式? [英] What PHP application design/design patterns do you use?

查看:92
本文介绍了您使用什么PHP应用程序设计/设计模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请与我分享您最喜欢的PHP应用程序设计/设计模式.我想知道的一些事情:

Please share your favorite application design / design patterns for use in PHP with me. Some things I'd like to know:

  • 文件夹的设计方式
  • 如何在PHP应用程序中使用对象定向
  • 您有处理CRUD,分页或其他任何常见任务的标准方法吗?
  • 如何避免使用重复代码?您对库/共享通用代码等的处理方式是什么?
  • 可以通过哪些方式使代码更优雅?

您不必全部回答,回答其中任何一个或几个都将有所帮助.

You don't have to answer all of these, answering any or a few of these will be helpful.

我之所以这么问,是因为我对用PHP编写重复的,丑陋的代码感到非常厌倦,并且想为我的自由职业项目创建一个小型框架,这将使编程变得更容易并使我专注于具有挑战性的工作. /业务任务,而不是表单验证,分页和其他平凡的活动,这些活动占PHP编程工作的80%

The reason I'm asking this,is because I'm very tired of writing repetitive, ugly code in PHP and I want to make a small framework for my freelancing projects which will make programming easier and let me focus on the challenging/business tasks rather than form validation, pagination, and the other mundane activities which make up 80% of programming work in PHP

所有意见表示赞赏!

推荐答案

对此我可能会被否决,但是如果您确实想编写自己的框架,那么我就去做吧,因为您将从中学到很多东西经验.这里提到的其他框架都很棒并且经过了测试,使用它们并不会导致错误的决定,但这是您的选择.

I might get voted down for this, but if you really do want to write your own framework, I say go for it because you will learn a lot from the experience. The other frameworks mentioned here are great and tested and you wouldn't be making a bad decision using them, but it's your choice.

在开始编写框架之前,请查看其他框架(以它们的语法,目录结构,命名架构,设计模式等为基础),并尝试弄清它们为什么这样做以及要做什么(如果有的话),做不同的事情.试用一些教程并试用其代码,制作一些示例应用程序.如果这样做之后,您不喜欢使用它们,那么请继续并开始计划您的框架,保持有效并改进无效的框架.

Before starting to write your framework, look at the other frameworks (at their syntax, directory structure, naming schema, design patterns, etc) and try to figure out why they did what they did and what, if anything, you would do differently. Try out a few tutorials and play with their code, make a few sample apps. If, after doing that, you don't like using them, then go ahead and start planning your framework, keeping what worked and improving what didn't.

如果您决定自己动手,请根据我的经验推荐以下几点:

If you do decide to roll your own, here are a few things I would recommend from my own experience:

  • 将安全放在首位-如果 你写一个数据访问层,使用 绑定参数.如果您写表格 类,防止CSRF和XSS. 赶上您的例外并处理您的 错误.确保您的PHP 环境很安全.不要尝试 自己加密 算法.如果你不专心 关于安全性,不值得写 您自己的框架.
  • 注释您的代码-您将需要 评论,以帮助您记住如何 您的代码会在一段时间后生效.一世 通常会发现docblock注释 绰绰有余.除此之外, 评论为什么您做了某件事,而不是 你做了什么.如果你需要解释 什么,您可能想重构.
  • 单一职责类别和 方法-您的大多数课程和 方法只能做一件事 一样东西.尤其要当心 这与数据库-您的 分页课不应该依赖 您的数据访问对象,也不应该 几乎任何其他(低级)类.
  • 单元测试-如果您使用的是每种方法 只做一件事,应该很远 更容易测试它们,它将 产生更好的代码.编写测试 首先,然后将代码传递给 测试.这也将为您带来更大的收益 以后自由重构的自由 破东西.
  • 抽象相似的类-如果您 有一个以上的班级 类似的事情,创建一个父类 使用之间的相似性 类并对其进行扩展.
  • 代理并模块化-如果您 编写验证系统(和 您可能会), 不要将每个验证器都包含为 一些超级验证中的方法 班级.将它们分开 类,并根据需要调用它们.这 可应用于许多领域: 过滤器,语言,算法, 验证器,等等.
  • 保护和私有化-在大多数情况下 情况下,最好使用getter和 setter方法,而不是允许 直接访问类变量.
  • 一致的API -如果您有一个 render()方法和draw()方法 在不同的地方做同样的事情 上课,选一个并继续学习 所有课程.保持顺序 参数与方法相同 使用相同的参数.一致的API是更简单的API.
  • 记住自动加载-该类 名称可能会有点笨拙, 很长,但是Zend命名的方式 对目录进行分类和组织 使自动加载变得更加容易. 更新:从PHP 5.3开始,您应该开始使用名称空间.
  • 从不回声或打印任何内容-给 它作为返回值,并让用户 决定是否应回显.很多 有时您会使用返回值 作为另一种方法的参数.
  • 不要试图解决世界难题 问题-首先解决您自己的问题.如果 您现在不需要功能, 就像一个用于数字本地化的类 或日期或货币,请不要写. 等待直到需要它.
  • 不要预先优化-构建一些 与您的简单应用程序 框架,然后对其进行微调. 否则,您可以花费很多 没效率的时间.
  • 使用源代码管理-如果您花费 无数小时创造了 杰作,不要冒险 迷路了.
  • Make Security Your Top Priority - If you write a data access layer, use bound parameters. If you write a form class, guard against CSRF and XSS. Catch your exceptions and handle your errors. Make sure that your PHP environment is secure. Don't try coming up with your own encryption algorithm. If you don't concentrate on security, it's not worth writing your own framework.
  • Comment Your Code - You will need comments to help you remember how your code works after a while. I usually find that docblock comments are more than enough. Beyond that, comment why you did something, not what you did. If you need to explain what, you may want to refactor.
  • Single Responsibility Classes and Methods - Most of your classes and methods should do one thing and only one thing. Especially watch out for this with the database - Your pagination class shouldn't rely on your data access object, nor should almost any other (low-level) class.
  • Unit Test - If each of your methods does only one thing, it should be far easier to test them and it will result in better code. Write the test first, then the code to pass the test. This will also give you greater freedom to refactor later on without breaking something.
  • Abstract Similar Classes - If you have more than one class that does similar things, create a parent class that uses the similarities between the classes and extend it.
  • Delegate and Modularize - If you're writing a validation system (and chances are you probably would), don't include each validator as a method in some super validation class. Separate them into individual classes and call them as needed. This can be applied in many areas: filters, languages, algorithms, validators, and so on.
  • Protect and Privatize - In most cases, it's better to use getter and setter methods rather than allowing direct access to class variables.
  • Consistent API - If you have a render() method and a draw() method that do the same things in different classes, pick one and go with it across all classes. Keep the order of the parameters the same for methods that use the same parameters. A consistent API is an easier API.
  • Remember Autoloading - The class names can get a little clunky and long, but the way Zend names the classes and organizes the directories makes autoloading a lot easier. Update: As of PHP 5.3, you should begin using namespaces.
  • Never echo or print anything - Give it as a return value and let the user decide if it should be echoed. A lot of times you'll use the return value as a parameter for another method.
  • Don't Try to Solve the World's Problems - Solve your own first. If you don't need a feature right now, like a class for localizing numbers or dates or currency, don't write it. Wait until you need it.
  • Don't Preoptimize - Build a few simple applications with your framework before fine tuning it. Otherwise, you can spend a lot of time on nothing productive.
  • Use Source Control - If you spend countless hours creating a masterpiece, don't risk it getting lost.

这篇关于您使用什么PHP应用程序设计/设计模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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