PHP Newbies: How to write good code [closed]

孤者浪人 提交于 2019-12-30 05:23:11

问题


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.


回答1:


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

  • Learn OWASP top 10
  • Learn PDO to prevent SQL-injections.
  • Learn Filter to prevent XSS.
  • Learn how to prevent CSRF.
  • Learn about http-only cookies and how to use in PHP.

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.

  • Learn to use distributed memory caching system like Redis or Memcached.
  • Learn to just tackle low hanging fruit and forget about silly micro-optimizations.

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.

  • Understand that PHP is just a tool in your toolbox and is not the best solution for all your problems. For example hanging requests(long-polling) can not be implemented efficiently in PHP and that's why Facebook decided to implement chat in Erlang instead of PHP.



回答2:


  • Learn how to do tracing (echo, print_r). Invaluable.
  • Check for errors after executing db queries (or better: write your own query function which automatically checks this. or still better: use some library for building queries).
  • Know the difference between result mysql resource, result set and result row.


来源:https://stackoverflow.com/questions/6685829/php-newbies-how-to-write-good-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!