【发布时间】:2020-04-14 14:38:05
【问题描述】:
从 CodeIgniter 3 升级到 CodeIgniter 4,一开始我发现了一个我无法解决的问题。
在 CI3 中,我在 application/config/config.php 中有一个 switch 语句来设置 $config['base_url']。
好像
$localFolder = "localfolder";
switch (ENVIRONMENT) {
case 'development':
$config['base_url'] = "http://$localFolder.loc";
break;
default:
$config['base_url'] = "http://testserver.com/$localFolder";
break;
}
但在 CI4 中,app/Config/App.php 现在是一个类,我还没有弄清楚如何根据 ENVIRONMENT 变量定义 public $baseURL = "samplefolder";。
立即调用函数不起作用:
public $baseURL = (function(){
switch (ENVIRONMENT) {
case 'development':
$this->baseURL = "http://$localFolder.loc";
break;
default:
$this->baseURL = "http://testserver.com/$localFolder";
break;
}
})();
错误:
Fatal error: Constant expression contains invalid operations
另外,用$this-> 声明后调用函数会产生错误:
public $baseURL = "";
public function baseURL($localFolder)
{
switch (ENVIRONMENT) {
case 'development':
$this->baseURL = "http://$localFolder.loc";
break;
default:
$this->baseURL = "http://testserver.com/$localFolder";
break;
}
}
$this->baseURL("localfolder");
错误:
Parse error: syntax error, unexpected '$this' (T_VARIABLE), expecting function (T_FUNCTION) or const (T_CONST)
我将不胜感激!
【问题讨论】:
标签: php codeigniter codeigniter-4