您使用哪些 PHP 应用程序设计/设计模式?

发布于 2024-07-13 14:18:27 字数 406 浏览 10 评论 0 原文

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

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

您不必回答所有这些问题,回答其中的任何一个或几个都会有帮助。

我问这个的原因是因为我厌倦了用 PHP 编写重复的、丑陋的代码,我想为我的自由项目制作一个小框架,这将使编程变得更容易,让我专注于具有挑战性的/业务任务而不是表单验证、分页和其他日常活动,这些活动占 PHP 编程工作的 80%

所有意见均表示赞赏!

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

  • How your folders are designed
  • How you use object oritentation in your PHP applications
  • Do you have a standard way of dealing with CRUD, pagination, or any other common tasks?
  • How do you avoid using repetitive code? What's your approach to libraries/sharing common code, etc?
  • What are ways in which you can make your code more elegant?

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

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

All opinions appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

谜兔 2024-07-20 14:18:27

我可能会被否决,但如果你真的想编写自己的框架,我建议你去做,因为你会从这次经验中学到很多东西。 这里提到的其他框架都很棒并且经过测试,使用它们您不会做出错误的决定,但这是您的选择。

在开始编写框架之前,请查看其他框架(查看它们的语法、目录结构、命名模式、设计模式等),并尝试找出它们为什么这样做,以及您会采取哪些不同的做法(如果有的话)。 尝试一些教程并使用他们的代码,制作一些示例应用程序。 如果这样做之后您不喜欢使用它们,那么请继续并开始规划您的框架,保留有效的内容并改进无效的内容。

如果您确实决定自己推出,我会根据自己的经验推荐以下几点:

  • 将安全性作为您的首要任务 - 如果
    你编写一个数据访问层,使用
    绑定参数。 如果你写一个表格
    类,防范CSRF和XSS。
    捕获您的异常并处理您的
    错误。 确保您的 PHP
    环境安全。 不要尝试
    提出你自己的加密
    算法。 如果你不集中注意力
    关于安全性,不值得写
    你自己的框架。
  • 评论您的代码 - 您将需要
    评论可帮助您记住如何
    你的代码在一段时间后就可以工作了。 我
    通常会发现docblock注释
    绰绰有余。 除此之外,
    评论你为什么做某事,而不是
    你做了什么。 如果你需要解释
    什么,你可能想重构。
  • 单一职责类和
    方法
    - 你的大部分课程和
    方法应该只做一件事
    一件事。 特别要注意的是
    这与数据库 - 您的
    分页类不应该依赖
    您的数据访问对象,也不应该
    几乎任何其他(低级)课程。
  • 单元测试 - 如果您的每种方法
    只做一件事,应该很远
    更容易测试它们,并且会
    产生更好的代码。 编写测试
    首先,然后是要传递的代码
    测试。 这也将为您带来更大的
    稍后可以自由重构,无需
    打破一些东西。
  • 抽象相似类 - 如果您
    有多个类可以
    类似的东西,创建一个父类
    它利用了之间的相似性
    类并扩展它。
  • 委托和模块化 - 如果您
    编写一个验证系统(以及
    你可能会的),
    不要将每个验证器包含为
    一些超级验证中的方法
    班级。 将它们分成单独的
    类并根据需要调用它们。 这
    可以应用在很多领域:
    过滤器、语言、算法、
    验证器等。
  • 保护和私有化 - 在大多数情况下
    在这种情况下,最好使用 getter 和
    setter 方法而不是允许
    直接访问类变量。
  • 一致的 API - 如果您有
    render() 方法和draw() 方法
    在不同的地方做同样的事情
    课程,选择一门并参加
    跨所有班级。 保持顺序
    方法的参数相同
    使用相同的参数。 一致的 API 是更简单的 API。
  • 记住自动加载 - 类
    名字可能会有点笨拙
    很长,但是 Zend 命名的方式
    类并组织目录
    使自动加载变得更加容易。 更新:从 PHP 5.3 开始,您应该开始使用命名空间。
  • 切勿回显或打印任何内容 - 给予
    它作为返回值并让用户
    决定是否应该回显。 很多
    您将使用返回值的次数
    作为另一个方法的参数。
  • 不要试图解决世界的问题
    问题
    - 首先解决你自己的问题。 如果
    您现在不需要某个功能,
    就像一个用于本地化数字的类
    或日期或货币,不要写。
    等到你需要的时候。
  • 不要预先优化 - 构建一些
    简单的应用程序与您
    框架,然后对其进行微调。
    不然你可能会花很多钱
    时间没有任何成效。
  • 使用源代码管理 - 如果您花费
    无数的时间创造
    杰作,不要冒险得到它
    丢失的。

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:

  • 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.
伪装你 2024-07-20 14:18:27

我必须同意上面的海报。 如果您在使用 PHP 编程时没有使用框架,那么您实际上是在双手被绑在背后进行编程。 我个人推荐 CodeIgniter。 它是最快的框架,非常容易学习,并且拥有非常活跃的社区。 该框架将回答您的所有问题:

* How your folders are designed

CodeIgniter(或任何与此相关的框架)将您的逻辑分为视图、模型和控制器,每个视图、模型和控制器都有自己的文件夹。

* Do you have a standard way of dealing with CRUD, pagination, or any other common tasks?

CI 有一个分页库,并且有像 DataMapper 这样的第三方库,用于以面向对象的方式 (ORM) 包装 CRUD 调用。

* What are ways in which you can make your code more elegant?

模型、视图和控制器的分离使得代码非常优雅。

(我没有回答的两个问题在使用框架时几乎是隐含的)

I have to agree with the above posters. If you're not using a framework when programming in PHP you're really programming with your hands tied behind your back. I personally recommend CodeIgniter. It's the fastest framework around, it's very easy to learn, and has a very active community. All of your questions will be answered by the framework:

* How your folders are designed

CodeIgniter (or any framework for that matter) separates your logic into views, models, and controllers, each with their own folder.

* Do you have a standard way of dealing with CRUD, pagination, or any other common tasks?

CI has a pagination library, and it has 3rd party libraries like DataMapper for wrapping your CRUD calls in an object oriented way (ORM).

* What are ways in which you can make your code more elegant?

The seperation of the model, view, and controller make for very elegant code.

(The 2 questions I didn't answer are pretty much implied when using the framework)

药祭#氼 2024-07-20 14:18:27

我想很多 php 开发人员都遵循了与我类似的路线:小脚本 -> 程序/内联代码 -> 可能看看模板 -> 面向对象编程-> 然后是一个框架。 我认为对于 PHP 开发人员来说,伴随着 PHP“成长”,学习设计模式以匹配当前版本可用的功能可能是很常见的。

MVC 是当今流行框架中最常用的设计模式。 CakePHP 是我选择的框架,尽管 SymphonyZend 也很受欢迎 - 非常值得尝试很少,很快就会发现您感觉最舒服的。

对于大多数项目(快速开发和可移植代码是优先考虑的),我使用 Cake,但是对于轻量级应用程序(我最近开发的一个是 好坏),您想要快速运行(在低规格硬件上)并且不需要大型框架之一的功能所增加的体积/重量,我建议阅读 Rasmus Lerdorf 关于他的文章无框架 PHP MVC 框架

基本上,如果您追求一种真正的面向对象语言,鼓励漂亮的代码和最佳设计实践,那么 PHP 总是会输给 Ruby、Python 和 C# 等语言。 但是,PHP 有其优点,例如不需要模板语言(它就是一种),PHP 可以运行得非常快且成本低廉,并且不需要为所有应用程序提供大型框架的重量。

我鼓励采用一种设计模式,该模式采用 MVC 等设计模式的可管理性,并将其与 PHP 的优势相结合。

I imagine a lot of php developers have followed a similar route to mine: small scripts -> procedural/inline-code -> possibly a look at templating -> OOP -> then a framework. I think it may be quite common for a PHP developer to have "grown up" with PHP, learning design patterns to match the features available with the current version.

MVC is the most frequently used design pattern in the popular frameworks being used today. CakePHP is my framework of choice although Symphony and Zend are very popular too – it's well worth trying out a few and it'll soon become apparent which you feel most comfortable with.

For most projects (where rapid development and portable code are the priorities) I use Cake, however for light weight apps (one I developed recently was Good Baad) that you'd like to run fast (on low spec hardware) and do not need the bulk/weight added by the functionality of one of the large frameworks I recommend reading Rasmus Lerdorf's article on his No Framework PHP MVC framework.

Basically if you're after a true object oriented language that encourages beautiful code and the best design practices PHP is always going to lose out to the likes of Ruby Python and C#. But, PHP has its strengths e.g. no need for a templating language (it is one), PHP can run very fast and cheaply and does not need the weight of a large framework for all applications.

I'd encourage adopting a design pattern that takes the manageability of a design pattern like MVC and combine it with PHP's strengths.

各自安好 2024-07-20 14:18:27

我几乎感觉像是打破了记录,但我建议您看一下一些常见的框架,原因有两个:

  1. 即使您选择不使用其中一些框架,其中一些也写得很好,设计得很好。 我特别喜欢 Zend Framework,但我稍后会回到这个话题。
  2. 问问自己为什么要重新发明轮子。 您是否真的觉得您比背后的社区(在此处插入选择的框架)更了解其他人面临的相同设计问题,以证明从头开始编写某些东西是合理的? 作为一个最初查看了几个框架并认为它们太大、提出了太多学习曲线或太多开销并因此开发了我自己的框架的人,我可以告诉你,如果你从头开始编写自己的框架是一个很大的痛苦,可以简单地使用一个可以轻松扩展的现有的。

说到使用可以轻松扩展的框架,我在 Zend Framework 方面获得了非常积极的体验。 它具有内聚性但松散耦合的结构使得可以快速轻松地扩展任何现有组件,并且整个框架的设计理念是您需要编写自己的帮助程序和插件类来添加其整体功能。

我发现 Zend Framework 非常灵活,以至于我运行一个网站,一部分是 Zend Framework MVC,一部分是我旧的蹩脚框架,甚至是我还没有重写的旧的蹩脚代码。 事实上,因为在重写过程中我们发现一个页面使用旧框架运行速度慢得令人无法接受,所以我已将单个页面切换为在 Zend Framework 架构下运行。

为了回答您的一些问题,我建议您查看 Martin Fowler 的《企业应用程序架构模式》。 他提供了许多关于如何解决许多常见问题的宝贵见解,例如如何在应用程序中创建数据库交互层。 Fowler 还涵盖了 MVC 和 Front Page Controller 等主题。

I almost feel like a broken record, but I would recommend that you take a look at some of the common frameworks for two reasons:

  1. Even if you choose not to use one, some of them are very well written and very well designed. I particularly like Zend Framework but I'll come back to that in a second.
  2. Ask yourself why you're reinventing the wheel. Do you really feel that you understand the same design problems everyone else faces so much better than the community behind (insert framework of choice here) to justify writing something from scratch? Speaking as one who originally looked at several frameworks and decided that they were too big, presented too much of a learning curve or too much overhead and so developed my own, I can tell you that writing your own from scratch is a big pain if you can simply use an existing one that can be easily extended.

Speaking of using a framework that can be easily extended, I have had very positive experiences with Zend Framework. It's cohesive and yet loosely coupled structure makes it possible to quickly and easily extend any existing component and the entire framework is designed around the idea that you will need to write your own helper and plugin classes to add to its overall functionality.

I've found Zend Framework to be so completely flexible that I am running a single website as part Zend Framework MVC and part my old crappy framework and even older crappier code that I haven't gotten to rewrite yet. In fact, because during our rewrite we found one page that ran unacceptably slow using the old framework, I've switched the single page to run under the Zend Framework architecture.

To answer some of your questions, I would recommend that you look into Patterns of Enterprise Application Architecture by Martin Fowler. He provides a lot of valuable insights into how to solve a number of these common problems like how to create a database interaction layer in your application. Fowler also covers topics like MVC and Front Page Controller.

你丑哭了我 2024-07-20 14:18:27

我已经解释了我的大部分 PHP 方法 此处

但现在,我只是尽可能使用 Django。

i've explained most of my PHP methodology here.

but nowadays, i just use Django everywhere i can.

转角预定愛 2024-07-20 14:18:27

当我第一次厌倦了混合代码和 html 时,我开始使用 smarty 模板引擎。 经过一段时间的 hack 后,我意识到编写自己的框架只是重复工作。

我用 Joomla 完成了一些项目,它实际上是一个 CMS,但它为客户提供了很多控制权内容。

最终我决定为我的项目使用一个真正的框架。 我正在使用 symfony,它受到 Rails 的启发并且有很好的文档记录,但我已经听说 cakePHPZendFramework也很不错。

I started out with the smarty templating engine when i first got tired of mixing code and html. After hacking for a while, I realized that writing my own framework is just duplicating work.

I've done a few projects with Joomla, which is really a CMS but it gives clients a lot of control over content.

Ultimately I've settled on using a real framework for my projects. I'm using symfony, which is inspired by Rails and is very well documented, but I've heard cakePHP and ZendFramework are also very good.

绻影浮沉 2024-07-20 14:18:27

我使用 Zend Framework,它几乎定义了文件夹布局和 OOP(MVC 范例)。 对于常见任务,例如分页,我使用 Zend_Paginator (我的模型类实现 Zend_Paginator_Adapter_Interface),对于验证,我使用 Zend_Validate 类等。谢谢这样我就可以完全专注于业务逻辑,而不是重新发明轮子。

I use Zend Framework, which pretty much defines folder layout and OOP (MVC paradigm). For common tasks, such as for example pagination I use Zend_Paginator (my model classes implement Zend_Paginator_Adapter_Interface), for validation I use Zend_Validate classes etc. Thanks to that I can fully concentrate on business logic instead of reinventing the wheel.

携余温的黄昏 2024-07-20 14:18:27

使用 Zend FrameworkDoctrine,我的文件夹结构通常是这样的:

root
  app
    config         (db config, routing config, misc config)
    doctrine       (fixtures, migrations, generated stuff, etc)
    lib
    logs
    models         (doctrine models)
    modules        (zend mvc modules)
    bootstrap.php
  docs             (db diagrams, specs, coding standards, various docs)
  pub              (web root)
  tests
  tools            (console tools, i.e. doctrine-cli)
  vendor           (zend and doctrine libraries, preferably as svn-externals)

Using Zend Framework and Doctrine, my folder structure usually looks like this:

root
  app
    config         (db config, routing config, misc config)
    doctrine       (fixtures, migrations, generated stuff, etc)
    lib
    logs
    models         (doctrine models)
    modules        (zend mvc modules)
    bootstrap.php
  docs             (db diagrams, specs, coding standards, various docs)
  pub              (web root)
  tests
  tools            (console tools, i.e. doctrine-cli)
  vendor           (zend and doctrine libraries, preferably as svn-externals)
红焚 2024-07-20 14:18:27

我已经有一段时间自己写东西了,每次我都无法抽出时间完全完成它,因为我被某些事情卡住了。

然后我开始意识到我做的事情是否正确。

因此,我放弃了自己编写一个受大众喜爱的程序:Zend。

我看了其他人,但似乎 Zend 已经存在了一段时间并且他们知道自己的事情。

MVC 也是我现在写的任何东西的前进方式。

I've been messing around writing my own stuff for a while now and everytime I can never get around to finishing it fully because I get stuck on something.

And then comes the part where I come to realization of whether I am doing something right.

And as such I have given up on writing my own an going with a crowd favourite: Zend.

I looked at others but it seems Zend has been around a while and they know their things.

MVC is also the way I am going forward with anything I write now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文