The art of PHP callback

the art of PHP callback

The art of PHP callback PHP callback are functions that may be called dynamically by PHP. They are used by native functions such as array_map, usort, preg_replace_callback, etc. Here is a reminder of the various ways to create a callback function in PHP, and use it with the native functions. String functions Strings are the […]

为什么静态审计帮助代码质量

静态审计是一个帮助提高代码质量的非常好的工具。审计意味着写下的代码被一个外部的审计师审查。人工的审计者通常是最好的,虽然他们不是总是适用。静态审计提供一个代码审查的自动的方法,并能很快地接收到反馈。它系统性地检查每一个文件的每一行代码。它不针对同人工审核一样的违规实例,但是也带来一系列的很好地优势:

在PHP中用好接口

  什么是接口? 接口,在PHP中也像在其他的面向对象(OOP)的语言中一样,定义在一个类中有哪些方法必须实现。接口给名字和参数以记录,但是没有内容:实际的(实现)代码将会要类来提供。 <?php interface movable { function move($distance) ; } class human implements movable { public function move($distance) { $this->walk($distance); } } class dog implements movable { public function move($distance) { $this->run($distance); // dogs never walk… } } class marsrover implements movable { public function move($distance) { for($i = 0; $i < $distance / […]

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 && […]