迁移到PHP5.4后,你得清理代码中的很多“坏习惯”。
扩展方法中的强类型参数定义
这意味着您必须在变量名之前编写方法接收到的对象的类。这只需要替换父类方法的方法,而不是所有方法。如果不确定,只需检查核心文件或 API 中父类中的方法声明即可。
// Behavior code
public function afterSave(Model $model, $created) //GOOD
function aferSave($model) //NOT GOOD
// Component code
public function shutdown(Controller $Controller) // GOOD
function shutdown($Controller) // BAD
移除“Call-time pass by reference”
我看到很多人通过引用在函数中传递 arround Controller 和 Model 对象:
function beforeSave(&$Model)
这会引发错误并且是错误的。
只需删除变量前的 & 符号。您不会破坏任何功能,因为对象已经在 PHP 中通过引用传递,据我所知,this was removed in PHP 5.4。
在扩展方法中声明所有方法参数
如果你覆盖了一个父类的方法,你应该在函数定义中声明所有的函数参数。如果缺少参数,会报错。
例子:
SomeBehavior extends ModelBehavior
public function afterSave(Model $Model){ } //WRONG, but works in 5.3
public function afterSave(Model $Model, $created){ } //RIGHT, works everwhere :)
添加 App::load() 以加载依赖项
检查您的类是否真的可用总是好的,因此请仔细检查您是否在文件开头使用App::load() 加载了所有依赖的类。