phpcakephpdatasource

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


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/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.


Solution

  • 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.