PHP开发的一些最佳模式和实践是什么?

时间:2021-03-30 20:03:24

I am mainly looking for good development practices specially when working in conjunction with mysql. I searched through the questions but could not find any related questions. I would appreciate if some one share their practices and wisdom gained through experience.

我主要是在与mysql一起工作时寻找良好的开发实践。我搜索了一些问题,但未找到任何相关问题。如果有人分享他们通过经验获得的实践和智慧,我将不胜感激。

Apart from some coding standards, I am also looking for design standards and common architectural practices.

除了一些编码标准,我还在寻找设计标准和常见的架构实践。

Background: I started my career with Java, and over the years I moved to C#/.NET space. I have been practicing architect for over 3 years now. Just added this to give some idea to people.

背景:我的职业生涯开始于Java,多年来我转到了C#/。NET领域。我已经练习建筑师超过3年了。只是添加了这个给人们一些想法。

5 个解决方案

#1


26  

I would suggest that you familiarize yourself with the history of PHP, I know that doing so has given me a much greater appreciation of what PHP is today and where it has come from.

我建议你熟悉PHP的历史,我知道这样做让我更加了解PHP的现状和来源。

In short, PHP was written by Rasmus Lerdorf to provide simple wrapper functions for the C code that was actually doing the heavy-lifting so that he could have a simpler language / syntax for writing templates that needed to behave dynamically. The growth of PHP and the community which surrounds it is best described as organic. And much like other things that grow organically, its more than a little messy, asymmetrical, and downright non-congruent.

简而言之,PHP是由Rasmus Lerdorf编写的,为C代码提供了简单的包装函数,实际上这些函数正在进行繁重的工作,因此他可以使用更简单的语言/语法来编写需要动态运行的模板。 PHP及其周围社区的发展最好被描述为有机的。就像其他有机生长的东西一样,它不仅有点凌乱,不对称,而且非常一致。

Once you understand PHP and its community, you need to embrace PHP for everything that it is and everything that it is not. This idea was best presented by Terry Chay in his article PHP without PHP. He's specifically talking about the concept of funky caching, but he captures the concept of coding for PHP as if it were PHP and not (insert favorite language here) better than anyone I've ever seen. In other words, don't try to make PHP into Java, C#, Ruby, etc because if you do you'll fail and you'll hate your life.

一旦理解了PHP及其社区,就需要拥抱PHP以及它所不具备的一切。这个想法最好由Terry Chay在他的PHP PHP文章中提出。他特别谈到了时髦缓存的概念,但他抓住了PHP编码的概念,好像它是PHP而不是(在这里插入最喜欢的语言)比我见过的任何人都更好。换句话说,不要试图将PHP变成Java,C#,Ruby等,因为如果你这样做,你就会失败而且你会讨厌你的生活。

Take a look at How is PHP Done the Right Way?.

看看PHP如何做正确的方法?

I must say that you must first, last, and always avoid the tendency of most beginning PHP developers to use the spaghetti-code anti-pattern. In other words, if you find that you're writing code that contains sql queries, manipulation of data, validation of data, and html output all in a single php script, then you're doing it wrong.

我必须说,你必须首先,最后,并始终避免大多数初学PHP开发人员使用意大利面条代码反模式的倾向。换句话说,如果你发现你在一个PHP脚本中编写包含sql查询,数据操作,数据验证和html输出的代码,那么你做错了。

In order to avoid this, it will be helpful to learn something about the nature of web-oriented design patterns. This of course precludes a familiarity with object-oriented programming. But once you've learned the basics of object-oriented programming in PHP, study the MVC design pattern. You don't have to implement this exactly but using the basic ideas of Model-View-Controller will allow you to avoid the blob script problem that most newbies tend to create.

为了避免这种情况,了解面向Web的设计模式的本质将会有所帮助。这当然妨碍了对面向对象编程的熟悉。但是,一旦你在PHP中学习了面向对象编程的基础知识,就可以研究MVC设计模式。您不必完全实现这一点,但使用模型 - 视图 - 控制器的基本思想将允许您避免大多数新手倾向于创建的blob脚本问题。

On this point, I would strongly recommend that you take any code snippets you find on the web with a grain of salt. And even if you find it in a book you'll have to consider how old the book is. PHP as a language has advanced quite a long ways and you can't just take code samples at face value because, depending on their age, they may be using workarounds that were valid in 3.x or 4.x but simply are no longer necessary with newer features.

在这一点上,我强烈建议您使用在网络上找到的任何代码片段。即使你在一本书中找到它,你也要考虑这本书的年龄。 PHP作为一种语言已经取得了很长的进步,你不能只是采用面值的代码样本,因为根据他们的年龄,他们可能会使用在3.x或4.x中有效的变通方法,但不再是必要的新功能。

One great thing to do is to study the various frameworks out there. Evaluate what you like and what you don't. Maybe even work up each of the quickstarts that are provided with the framework documentation so that you can start to get an idea of what you like and don't like. And I would strongly recommend that you review the code from the frameworks as well as several other open-source projects so that you can get a feel for how others do things in PHP. Again, take it all with a grain of salt because every PHP developer has their own pet peeves and nuances and none of us is right all the time. In fact, most of the time with PHP there are going to be several pretty good ways to do something.

一件好事是研究那里的各种框架。评估你喜欢什么,不喜欢什么。甚至可以处理随框架文档提供的每个快速入门,以便您可以开始了解您喜欢和不喜欢的内容。我强烈建议您查看框架中的代码以及其他几个开源项目,以便了解其他人如何在PHP中执行操作。再说一遍,因为每个PHP开发人员都有自己的烦恼和细微差别,所以我们都不是一直都是正确的。事实上,大多数时候使用PHP会有几种很好的方法可以做某事。

If you want to get a better understanding of the patterns that are being implemented by the frameworks and are commonly thrown around in the common vernacular on SO, I would suggest that you read Fowler and GoF. They'll teach all about the basic design patterns you'll use in your development efforts.

如果你想更好地理解框架正在实现的模式,并且通常在SO的常用白话中抛出,我建议你阅读Fowler和GoF。他们将教授您将在开发工作中使用的基本设计模式。

Specifically watch for the following:

特别注意以下内容:

  • Function files that contain LOTS of functions. This is most likely representative of a need to either put functions directly in the scripts that need them or it may also indicate an opportunity to create some more generic functions that can be made to fulfill the roles of a couple of highly specific functions. Of course, if you're building cohesive, well-encapsulated classes you shouldn't run into this problem.
  • 包含大量功能的函数文件。这很可能代表需要将函数直接放在需要它们的脚本中,或者它也可能表示有机会创建一些更通用的函数,这些函数可以用来完成几个高度特定的函数的作用。当然,如果你正在构建内聚的,封装良好的类,你不应该遇到这个问题。
  • The do everything class. This is a blob anti-pattern and is really nasty. In this case you need to identify where cohesion and encapsulation are breaking down and use those points as opportunities to break up the class into several smaller, more maintainable classes.
  • 尽一切力量。这是一个blob反模式,真的很讨厌。在这种情况下,您需要确定内聚和封装正在分解的位置,并将这些点用作将类拆分为几个更小,更易维护的类的机会。
  • SQL queries that don't use parameterized queries or at least escaped parameters. Very, very, very bad.
  • 不使用参数化查询或至少是转义参数的SQL查询。非常非常非常糟糕。
  • Any instance where validation is not being performed or is only performed client-side. When developing for the web, the only way to keep your site and your users safe is to assume that everyone else is a black hat.
  • 任何未执行验证或仅在客户端执行验证的实例。在开发网络时,保持网站和用户安全的唯一方法是假设其他人都是黑帽子。
  • A sudden obsessive desire to use a template engine. PHP is a templating language. Make certain that you have clear reasons for adding another layer onto your website before using a template engine.
  • 突然强迫使用模板引擎的愿望。 PHP是一种模板语言。在使用模板引擎之前,请确保您有明确的理由在网站上添加其他图层。

For further reading please look at the following:

如需进一步阅读,请查看以下内容:

PHP Application Design Patterns
Defend PHP -- useful to give you an idea of the most common criticisms.
Security of strip_tags and mysqlirealescapestring
What should Every PHP Programmer Know
How to Structure an ORM
Best Way to Organize Class Hierarchy
Main Components / Layers of PHP App
Why use Framework for PHP
Recommended Security Training for PHP

PHP应用程序设计模式保护PHP - 有助于您了解最常见的批评。 strip_tags和mysqlirealescapestring的安全性每个PHP程序员应该知道如何构建ORM的最佳方法组织类层次结构PHP应用程序的主要组件/层为什么使用Framework for PHP推荐的PHP安全培训

#2


10  

  • Use a coding standard.
  • 使用编码标准。
  • Use unit testing. PHPUnit and SimpleTest are the major xUnit systems in PHP.
  • 使用单元测试。 PHPUnit和SimpleTest是PHP中的主要xUnit系统。
  • Be object oriented.
  • 面向对象。
  • Use version control. Any version control, just use it.
  • 使用版本控制。任何版本控制,只需使用它。
  • If applicable, use a framework. Zend, CodeIgniter, Symfony, and CakePHP are the major ones.
  • 如果适用,请使用框架。 Zend,CodeIgniter,Symfony和CakePHP是主要的。
  • If no framework, at least use an ORM. Propel and Doctrine are the major ones.
  • 如果没有框架,至少使用ORM。 Propel和Doctrine是主要的。
  • Document. Heavily. Use PHPdoc or similar.
  • 文件。重。使用PHPdoc或类似的。

There are a wealth of tools out there for PHP. Please use them, and write good maintainable code. You'll make everyone happier.

PHP有很多工具可供使用。请使用它们,并编写良好的可维护代码。你会让每个人都更快乐。

#3


3  

Use PDO or mysqli. Using one of these will give you prepared statements, which are safer and more efficient. I can't believe how many examples and tutorials I see using the ancient mysql interfaces. PDO would also make it much easier to switch to a different database system, should you decide to try postgres for instance.

使用PDO或mysqli。使用其中一个将为您提供准备好的语句,这些语句更安全,更高效。我无法相信我使用古老的mysql接口看到了多少示例和教程。如果您决定尝试使用postgres,PDO也可以更容易切换到不同的数据库系统。

You might look into using Doctrine (http://www.doctrine-project.org). It has a bit of it's own learning curve, but provides very convenient functionality. The handiest parts, for me, are the table creation/test data loading functions. Personally, I prefer to write my own SQL and execute it with PDO, and not use an ORM much in production.

您可以考虑使用Doctrine(http://www.doctrine-project.org)。它有一些自己的学习曲线,但提供了非常方便的功能。对我来说,最方便的部分是表创建/测试数据加载功能。就个人而言,我更喜欢编写自己的SQL并使用PDO执行它,而不是在生产中使用ORM。

Mainly, learn about SQL and MySQL. http://www.kitebird.com/mysql-book/ this book is excellent. The PHP aspect isn't very intense; PDO takes care of most of it.

主要是,了解SQL和MySQL。 http://www.kitebird.com/mysql-book/这本书很棒。 PHP方面不是很激烈; PDO负责大部分工作。

#4


1  

Zend Framework's "Coding Standards for PHP" is good starting point. Check also this post.

Zend Framework的“PHP编码标准”是一个很好的起点。也请查看这篇文章。

#5


0  

Your from a Java background, and much of the OO stuff in PHP is very Javaesque. This means many of the design patterns you (hopefully) learnt in Java also apply (to a lesser extent) in PHP. An example for database access would be the DataMapper pattern.

您是Java背景的,PHP中的大部分OO都是Javaesque。这意味着您(希望)在Java中学习的许多设计模式也适用于(在较小程度上)PHP。数据库访问的一个示例是DataMapper模式。

#1


26  

I would suggest that you familiarize yourself with the history of PHP, I know that doing so has given me a much greater appreciation of what PHP is today and where it has come from.

我建议你熟悉PHP的历史,我知道这样做让我更加了解PHP的现状和来源。

In short, PHP was written by Rasmus Lerdorf to provide simple wrapper functions for the C code that was actually doing the heavy-lifting so that he could have a simpler language / syntax for writing templates that needed to behave dynamically. The growth of PHP and the community which surrounds it is best described as organic. And much like other things that grow organically, its more than a little messy, asymmetrical, and downright non-congruent.

简而言之,PHP是由Rasmus Lerdorf编写的,为C代码提供了简单的包装函数,实际上这些函数正在进行繁重的工作,因此他可以使用更简单的语言/语法来编写需要动态运行的模板。 PHP及其周围社区的发展最好被描述为有机的。就像其他有机生长的东西一样,它不仅有点凌乱,不对称,而且非常一致。

Once you understand PHP and its community, you need to embrace PHP for everything that it is and everything that it is not. This idea was best presented by Terry Chay in his article PHP without PHP. He's specifically talking about the concept of funky caching, but he captures the concept of coding for PHP as if it were PHP and not (insert favorite language here) better than anyone I've ever seen. In other words, don't try to make PHP into Java, C#, Ruby, etc because if you do you'll fail and you'll hate your life.

一旦理解了PHP及其社区,就需要拥抱PHP以及它所不具备的一切。这个想法最好由Terry Chay在他的PHP PHP文章中提出。他特别谈到了时髦缓存的概念,但他抓住了PHP编码的概念,好像它是PHP而不是(在这里插入最喜欢的语言)比我见过的任何人都更好。换句话说,不要试图将PHP变成Java,C#,Ruby等,因为如果你这样做,你就会失败而且你会讨厌你的生活。

Take a look at How is PHP Done the Right Way?.

看看PHP如何做正确的方法?

I must say that you must first, last, and always avoid the tendency of most beginning PHP developers to use the spaghetti-code anti-pattern. In other words, if you find that you're writing code that contains sql queries, manipulation of data, validation of data, and html output all in a single php script, then you're doing it wrong.

我必须说,你必须首先,最后,并始终避免大多数初学PHP开发人员使用意大利面条代码反模式的倾向。换句话说,如果你发现你在一个PHP脚本中编写包含sql查询,数据操作,数据验证和html输出的代码,那么你做错了。

In order to avoid this, it will be helpful to learn something about the nature of web-oriented design patterns. This of course precludes a familiarity with object-oriented programming. But once you've learned the basics of object-oriented programming in PHP, study the MVC design pattern. You don't have to implement this exactly but using the basic ideas of Model-View-Controller will allow you to avoid the blob script problem that most newbies tend to create.

为了避免这种情况,了解面向Web的设计模式的本质将会有所帮助。这当然妨碍了对面向对象编程的熟悉。但是,一旦你在PHP中学习了面向对象编程的基础知识,就可以研究MVC设计模式。您不必完全实现这一点,但使用模型 - 视图 - 控制器的基本思想将允许您避免大多数新手倾向于创建的blob脚本问题。

On this point, I would strongly recommend that you take any code snippets you find on the web with a grain of salt. And even if you find it in a book you'll have to consider how old the book is. PHP as a language has advanced quite a long ways and you can't just take code samples at face value because, depending on their age, they may be using workarounds that were valid in 3.x or 4.x but simply are no longer necessary with newer features.

在这一点上,我强烈建议您使用在网络上找到的任何代码片段。即使你在一本书中找到它,你也要考虑这本书的年龄。 PHP作为一种语言已经取得了很长的进步,你不能只是采用面值的代码样本,因为根据他们的年龄,他们可能会使用在3.x或4.x中有效的变通方法,但不再是必要的新功能。

One great thing to do is to study the various frameworks out there. Evaluate what you like and what you don't. Maybe even work up each of the quickstarts that are provided with the framework documentation so that you can start to get an idea of what you like and don't like. And I would strongly recommend that you review the code from the frameworks as well as several other open-source projects so that you can get a feel for how others do things in PHP. Again, take it all with a grain of salt because every PHP developer has their own pet peeves and nuances and none of us is right all the time. In fact, most of the time with PHP there are going to be several pretty good ways to do something.

一件好事是研究那里的各种框架。评估你喜欢什么,不喜欢什么。甚至可以处理随框架文档提供的每个快速入门,以便您可以开始了解您喜欢和不喜欢的内容。我强烈建议您查看框架中的代码以及其他几个开源项目,以便了解其他人如何在PHP中执行操作。再说一遍,因为每个PHP开发人员都有自己的烦恼和细微差别,所以我们都不是一直都是正确的。事实上,大多数时候使用PHP会有几种很好的方法可以做某事。

If you want to get a better understanding of the patterns that are being implemented by the frameworks and are commonly thrown around in the common vernacular on SO, I would suggest that you read Fowler and GoF. They'll teach all about the basic design patterns you'll use in your development efforts.

如果你想更好地理解框架正在实现的模式,并且通常在SO的常用白话中抛出,我建议你阅读Fowler和GoF。他们将教授您将在开发工作中使用的基本设计模式。

Specifically watch for the following:

特别注意以下内容:

  • Function files that contain LOTS of functions. This is most likely representative of a need to either put functions directly in the scripts that need them or it may also indicate an opportunity to create some more generic functions that can be made to fulfill the roles of a couple of highly specific functions. Of course, if you're building cohesive, well-encapsulated classes you shouldn't run into this problem.
  • 包含大量功能的函数文件。这很可能代表需要将函数直接放在需要它们的脚本中,或者它也可能表示有机会创建一些更通用的函数,这些函数可以用来完成几个高度特定的函数的作用。当然,如果你正在构建内聚的,封装良好的类,你不应该遇到这个问题。
  • The do everything class. This is a blob anti-pattern and is really nasty. In this case you need to identify where cohesion and encapsulation are breaking down and use those points as opportunities to break up the class into several smaller, more maintainable classes.
  • 尽一切力量。这是一个blob反模式,真的很讨厌。在这种情况下,您需要确定内聚和封装正在分解的位置,并将这些点用作将类拆分为几个更小,更易维护的类的机会。
  • SQL queries that don't use parameterized queries or at least escaped parameters. Very, very, very bad.
  • 不使用参数化查询或至少是转义参数的SQL查询。非常非常非常糟糕。
  • Any instance where validation is not being performed or is only performed client-side. When developing for the web, the only way to keep your site and your users safe is to assume that everyone else is a black hat.
  • 任何未执行验证或仅在客户端执行验证的实例。在开发网络时,保持网站和用户安全的唯一方法是假设其他人都是黑帽子。
  • A sudden obsessive desire to use a template engine. PHP is a templating language. Make certain that you have clear reasons for adding another layer onto your website before using a template engine.
  • 突然强迫使用模板引擎的愿望。 PHP是一种模板语言。在使用模板引擎之前,请确保您有明确的理由在网站上添加其他图层。

For further reading please look at the following:

如需进一步阅读,请查看以下内容:

PHP Application Design Patterns
Defend PHP -- useful to give you an idea of the most common criticisms.
Security of strip_tags and mysqlirealescapestring
What should Every PHP Programmer Know
How to Structure an ORM
Best Way to Organize Class Hierarchy
Main Components / Layers of PHP App
Why use Framework for PHP
Recommended Security Training for PHP

PHP应用程序设计模式保护PHP - 有助于您了解最常见的批评。 strip_tags和mysqlirealescapestring的安全性每个PHP程序员应该知道如何构建ORM的最佳方法组织类层次结构PHP应用程序的主要组件/层为什么使用Framework for PHP推荐的PHP安全培训

#2


10  

  • Use a coding standard.
  • 使用编码标准。
  • Use unit testing. PHPUnit and SimpleTest are the major xUnit systems in PHP.
  • 使用单元测试。 PHPUnit和SimpleTest是PHP中的主要xUnit系统。
  • Be object oriented.
  • 面向对象。
  • Use version control. Any version control, just use it.
  • 使用版本控制。任何版本控制,只需使用它。
  • If applicable, use a framework. Zend, CodeIgniter, Symfony, and CakePHP are the major ones.
  • 如果适用,请使用框架。 Zend,CodeIgniter,Symfony和CakePHP是主要的。
  • If no framework, at least use an ORM. Propel and Doctrine are the major ones.
  • 如果没有框架,至少使用ORM。 Propel和Doctrine是主要的。
  • Document. Heavily. Use PHPdoc or similar.
  • 文件。重。使用PHPdoc或类似的。

There are a wealth of tools out there for PHP. Please use them, and write good maintainable code. You'll make everyone happier.

PHP有很多工具可供使用。请使用它们,并编写良好的可维护代码。你会让每个人都更快乐。

#3


3  

Use PDO or mysqli. Using one of these will give you prepared statements, which are safer and more efficient. I can't believe how many examples and tutorials I see using the ancient mysql interfaces. PDO would also make it much easier to switch to a different database system, should you decide to try postgres for instance.

使用PDO或mysqli。使用其中一个将为您提供准备好的语句,这些语句更安全,更高效。我无法相信我使用古老的mysql接口看到了多少示例和教程。如果您决定尝试使用postgres,PDO也可以更容易切换到不同的数据库系统。

You might look into using Doctrine (http://www.doctrine-project.org). It has a bit of it's own learning curve, but provides very convenient functionality. The handiest parts, for me, are the table creation/test data loading functions. Personally, I prefer to write my own SQL and execute it with PDO, and not use an ORM much in production.

您可以考虑使用Doctrine(http://www.doctrine-project.org)。它有一些自己的学习曲线,但提供了非常方便的功能。对我来说,最方便的部分是表创建/测试数据加载功能。就个人而言,我更喜欢编写自己的SQL并使用PDO执行它,而不是在生产中使用ORM。

Mainly, learn about SQL and MySQL. http://www.kitebird.com/mysql-book/ this book is excellent. The PHP aspect isn't very intense; PDO takes care of most of it.

主要是,了解SQL和MySQL。 http://www.kitebird.com/mysql-book/这本书很棒。 PHP方面不是很激烈; PDO负责大部分工作。

#4


1  

Zend Framework's "Coding Standards for PHP" is good starting point. Check also this post.

Zend Framework的“PHP编码标准”是一个很好的起点。也请查看这篇文章。

#5


0  

Your from a Java background, and much of the OO stuff in PHP is very Javaesque. This means many of the design patterns you (hopefully) learnt in Java also apply (to a lesser extent) in PHP. An example for database access would be the DataMapper pattern.

您是Java背景的,PHP中的大部分OO都是Javaesque。这意味着您(希望)在Java中学习的许多设计模式也适用于(在较小程度上)PHP。数据库访问的一个示例是DataMapper模式。

相关文章