How to override model's constructor correctly in CakePHP

风格不统一 提交于 2019-11-30 22:44:31

why don't you look into the core code its open source after all: https://github.com/cakephp/cakephp/blob/2.1/lib/Cake/Model/Model.php#L653

so for all your models:

public function __construct($id = false, $table = null, $ds = null) {
    parent::__construct($id, $table, $ds);
}

Rather than override the constructor, how about using beforeFilter() for controllers or the before methods for the Model such as beforeFind(), beforeValidate(), etc.

One way that I found is to do something like this:

function __construct() {
    call_user_func_array(array('parent', '__construct'), func_get_args());
}

It allows you to not have to worry about what the parent gets passed. Although that code is pretty hideous.

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