PHP Newbies: How to write good code [closed]

2019-02-11 05:14发布

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.

2条回答
聊天终结者
2楼-- · 2019-02-11 05:24

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.

查看更多
3楼-- · 2019-02-11 05:24
  • 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.
查看更多
登录 后发表回答