我必须将parent::__construct($config) 放入我的CakePHP 数据源中吗?

问题描述 投票:0回答:1

有充分的理由放吗

parent::__construct($config)

在我正在开发的 CakePHP 数据源的构造中?我看到它在 https://github.com/cakephp/datasources/blob/master/models/datasources/amazon_associates_source.php 中找到的一些数据源中使用,但不确定为什么。我可以这样做

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

并以同样的方式访问我的 $config。

php cakephp datasource
1个回答
2
投票

如果你看一下 CakePHP 中的 DataSource 类,它的构造函数调用 setConfig 方法。这是setConfig方法源码:

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

可以看到它会合并几个配置。因此,您可以在类中定义 $config 属性,它将与用户提供给构造函数的任何内容合并。当然你可以在构造函数中这样做:

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

但是调用父构造函数将确保您的类遵循 CakePHP 在 DataSource 类中所做的任何更改。

© www.soinside.com 2019 - 2024. All rights reserved.