PHP里的神奇数字

神奇数字式一个没有解释其意义的字面值,而且它出现在代码里多次。这样的值将会显示没有任何意图,它应该被替换为常量:常量的名字将会使代码更可读,在未来更加容易更新。 ç 让我们来看看一些代码,并且把它的神奇数字分离出来来试图理解它的情况。这是一个真实的数字的例子,来源于最近发布的一个关于120 kLOC的开源应用。       Value Number of occurrences 0.0 1 1.0E-08 1 0.05 1 0.33 1 1.0 10 1.7 4 2.0 1 2.3 1 3.2 1 4.0 1 4.199999 1 4.299999 1 5.0 1 5.2 2 7.3 1 8.0 1 365.25 3 3600.0 1 用眼快速地扫一下显示: 0.33 看上去像是 1/3, 有点取一个近似值。它可以被取代为用被除数3的除法。 365.25 看上去像一个一年的时间用天表示。另外一个可疑的地方时整数365和366在应用中使用了多次。 几个整数被表达为实数(0, […]

更好的PHP Echo

你能在下面这行代码中找到多少可以改进的地方呢? echo (“this “.$will.” be displayed.”); Echo不需要小括号 首先,可能也是最明显的是那个无用的小括号。这个echo是一个“语言结构”(structure of language),也叫做PHP一个特殊的函数:在包括其他的一些特点中,一个特点是它不需要任何的小括号。这个其实也适用于inclusions,return和print。上面的代码和下面的代码实现一样的目的:

Isolated Blocks

Isolated blocks 4 % of PHP code has a strange sickness : isolated block. Blocks are a sequence of instructions that are grouped together in the code : blocks are marked with curly braces, and they are usually found after a flow control command, such as if…then, for or switch to group several instruction under the same branching. […]

PHP skips some code

PHP skips some code When PHP skips some code, it features code optimizations : it skips automatically an instruction, as soon as it detects useless job. No need for code cache, this is plain vanilla PHP. Implied if The classic case is when using logical operators && or ||. When the first operand of an && […]

New in PHP 7

New in PHP 7

New in PHP 7 Let’s review the new constants, functions, classes, interfaces and trait that PHP 7 features. We have reviewed the core of PHP (as of June 1rst) and spotted quite some new structures that are already available to you if you’re testing the water with PHP7. New Constants in PHP 7 In the core, […]

Code expiration

How code dies Code is born, grows, matures, decays and then dies. It may be seen as a living matter, just like the tulips in my garden. It is scary to transpose that image to code: little typo appearing, pieces of code disappearing, constants that changes their value over time (code inflation?), part of code […]

Magic numbers in PHP

Magic number is a literal value with unexplained meaning, that appears in the code multiple times. Such values will show no intent, and should be replace by a constant: the name of the constant will make the code more readable, and easier to update in the future. Hunting for Magic Numbers Let’s take a look […]

PHP Code Review Tools comparison : 4 tools of the market against Exakat

Why to use automated PHP Code Analysis Tools ? Traditional PHP Code Review demands an army of developers. Doing go PHP Code Review demands scarce experts. Sustainable PHP Code review demands tools that each uses to help manage the code. With the increasingly important of technical debt, in PHP as elsewhere, we’ve seen the last two […]

Well used interfaces in PHP

  What are interfaces ? Interfaces, in PHP as in all other OOP languages, specify what methods must be implemented in a class. Interfaces gives names and arguments counts, but no content : the actual code will be provided by the class. <?php interface movable {   function move($distance) ; } class human implements movable {   public function move($distance) […]