从 CodeIgniter 3 升级到 CodeIgniter 4,一开始我发现了一个我无法解决的问题。在 CI3 中,我有一个 switch 语句application/config/config.php来设置$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现在是一个类,我还没有弄清楚如何public $baseURL = "samplefolder";根据ENVIRONMENT变量定义。
立即调用函数不起作用:
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
此外,在声明 with 后调用该函数$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)
我将不胜感激任何帮助!
莫回无
守着一只汪
杨__羊羊