开发者

Do I have to put parent::__construct($config) in my CakePHP data source?

开发者 https://www.devze.com 2023-02-05 08:25 出处:网络
Is there a good reason to put parent::__construct($config) in the construct of a CakePHP data source I am developing? I see it being used in some of the data sources found in https://github.com/cak

Is there a good reason to put

parent::__construct($config)

in the construct of a CakePHP data source I am developing? I see it being used in some of the data sources found in https://github.com/cakephp/datasources/blob/master/models/datasources/开发者_Python百科amazon_associates_source.php but not sure why. I could just do

private $_config = array();
function construct($config){
    $this->_config = $config;
}

and access my $config the same way.


If you look at DataSource class in CakePHP, it's constructor call setConfig method. Here is setConfig method source:

function setConfig($config = array()) {
    $this->config = array_merge($this->_baseConfig, $this->config, $config);
}

You can see that it will merge several configuration. So you can define $config attributes in your class and it will merge with whatever user give to constructor. Of course you can do this in constructor:

function __construct($config){
    $this->setConfig($config);
}

But calling parent constructor will ensure your class to follow whatever change CakePHP made in DataSource class.

0

精彩评论

暂无评论...
验证码 换一张
取 消