PHP新手:如何编写好的代码 [英] PHP Newbies: How to write good code

查看:128
本文介绍了PHP新手:如何编写好的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于许多PHP相关的问题在这里非常基本,我建议准备一个提示和技巧的集合。



这可能是一个起点:





问题是:PHP新手应该做什么?



UPDATE-1



由于有些人审核了此问题,我会建议重新打开。请点击以下相应链接。

解决方案

测试



应该更好地测试您的代码,可能会练习 TDD 。您可以通过 PHPUnit 执行此操作。请记住解除Bob的三条规则来练习TDD。



  1. 除非要进行失败的单元测试通过,否则不允许编写任何生产代码。

  2. 你不能写出比足以失败的单位测试更多;



您必须首先为
打算编写的功能编写一个单元测试。但是根据规则2,你不能写很多单位
test。一旦单元测试代码编译失败或
断言失败,您必须停止并编写生产代码。但是根据规则3你
只能写生产代码,使测试编译或
通过,没有更多。



如果你想这将你意识到,你根本不能写
非常多的代码,而不编译和执行的东西。
的确,这是真的。在我们所做的一切,无论是写
测试,编写生产代码还是重构,我们始终保持系统
的执行。运行测试之间的时间是命令
的秒,或分钟。即使10分钟过长。


您应该尝试拥有高代码覆盖率。 PHPUnit还可以执行代码覆盖率分析,感谢 xdebug 重构代码 smelly list a>)应该很容易,因为您的测试用例已经存在。



安全





效果






缓存PHP脚本的编译字节码以避免开销
解析和编译每个请求的源代码(某些或所有的
,甚至可能永远不会被执行)。为了进一步提高性能,
缓存代码存储在共享内存中,并从
直接执行,从而最大限度地减少
运行时的缓慢磁盘读取和内存复制。







着名的引语我们应该忘记小的效率,说
大约97%的时间:过早的优化是所有的根
邪恶,by Donald Knuth, 6 也被错误地归因于
Hoare(由Knuth自己),虽然Hoare





Since many PHP-related questions are very basic here, I'd propose to prepare a collection of tips and tricks.

This might be a starting point:

  • Check as much input parameters of as much methods as possible (see assert()).
  • Log all errors to a log-file and visualize it using your admin backend (see set_error_handler()).
  • Use type-hints as often as possible (see type-hinting)
  • Set the error level to the absolute maximum. Then code in such a way, that not a single warning appears (see error_reporting()).
  • Learn why and how PHP implements and converts data types (see type juggling and string conversions)

Thus the question is: What should PHP newbies better do?

UPDATE-1

Since quite some people reviewed this question, I'd propose to reopen it. Please click on the respective link below.

解决方案

Test

You should better test your code, probably practicing TDD. You can do this thanks to PHPUnit. Keep in mind Uncle Bob's three rules to practice TDD.

  1. You are not allowed to write any production code unless it is to make a failing unit test pass.
  2. You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.
  3. You are not allowed to write any more production code than is sufficient to pass the one failing unit test.

You must begin by writing a unit test for the functionality that you intend to write. But by rule 2, you can't write very much of that unit test. As soon as the unit test code fails to compile, or fails an assertion, you must stop and write production code. But by rule 3 you can only write the production code that makes the test compile or pass, and no more.

If you think about this you will realize that you simply cannot write very much code at all without compiling and executing something. Indeed, this is really the point. In everything we do, whether writing tests, writing production code, or refactoring, we keep the system executing at all times. The time between running tests is on the order of seconds, or minutes. Even 10 minutes is too long.

You should try to have high code coverage. PHPUnit can also do code coverage analyses thanks to xdebug. Refactoring code that is smelly(list) should be easy, because of your test-cases which are already present.

Security

Performance

  • Learn and use APC

Caching the compiled bytecode of PHP scripts to avoid the overhead of parsing and compiling source code on each request (some or all of which may never even be executed). To further improve performance, the cached code is stored in shared memory and directly executed from there, minimizing the amount of slow disk reads and memory copying at runtime.

The famous quotation, "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil", by Donald Knuth,6 has also been mistakenly attributed to Hoare (by Knuth himself), although Hoare disclaims authorship.

这篇关于PHP新手:如何编写好的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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