没有构造函数的配置 Yii::WebApplication
Config Yii::WebApplication without Constructor
在 Yii 1.1 的 bootstrap 中(app/index.php,第 13 行),我们使用 Yii::createWebApplication($config)->run();
命令启动应用程序,createWebApplication
方法是(framework/YiiBase.php, 第 96 行)
public static function createWebApplication($config=null)
{
return self::createApplication('CWebApplication',$config);
}
这样 createWebApplication
方法是(framework/YiiBase,第 123 行)
public static function createApplication($class,$config=null)
{
return new $class($config);
}
所以 createApplication('CWebApplication',$config)
解释成 new CWebApplication($config);
这样的东西,嗯?但是在 CWebApplication
class 上,我们没有任何构造函数! (framework/yiilite.php,第 1622 行)。
现在的问题是: WebApplication 如何获取 $config
变量的配置并适应它?
下面的函数将returnreturn new CWebApplication($config);
public static function createApplication($class,$config=null)
{
return new $class($config);
}
如果你检查 CWebApplication class 扩展 CApplication,
CWebApplication.php
class CWebApplication extends CApplication
{
...
在 CApplication 中你会发现初始化的构造方法,framework/base/CApplication.php
abstract class CApplication extends CModule
{
....
public function __construct($config=null)
{
Yii::setApplication($this);
// set basePath at early as possible to avoid trouble
if(is_string($config))
$config=require($config);
if(isset($config['basePath']))
{
$this->setBasePath($config['basePath']);
unset($config['basePath']);
}
else
$this->setBasePath('protected');
Yii::setPathOfAlias('application',$this->getBasePath());
Yii::setPathOfAlias('webroot',dirname($_SERVER['SCRIPT_FILENAME']));
if(isset($config['extensionPath']))
{
$this->setExtensionPath($config['extensionPath']);
unset($config['extensionPath']);
}
else
Yii::setPathOfAlias('ext',$this->getBasePath().DIRECTORY_SEPARATOR.'extensions');
if(isset($config['aliases']))
{
$this->setAliases($config['aliases']);
unset($config['aliases']);
}
$this->preinit();
$this->initSystemHandlers();
$this->registerCoreComponents();
$this->configure($config);
$this->attachBehaviors($this->behaviors);
$this->preloadComponents();
$this->init();
}
abstract class CApplication
{
protected $config=array();
public function __construct($config=null) {
echo "CApplication::construct() called";
// DO anyting with config arrray which is from /protected/config/main.php
//$this->config = $config;
}
}
CWebApplication继承了CApplication所以也继承了Constructor方法。
要了解这个想法,您可以查看我下面的示例。
Edit
abstract class CApplication
{
protected $config=array();
public function __construct($config=null) {
echo "CApplication::construct() called";
// DO anyting with config arrray which is from /protected/config/main.php
//$this->config = $config;
}
}
class CWebApplication extends CApplication
{
public function test(){
echo "<pre>";
print_r($this->config);
echo "</pre>";
}
}
$config = array(
"name" =>"My Web Application",
"components" => array(
"db" => "connectionstring:..",
"urlManage"=> array(),
)
);
$obj = new CWebApplication($config);
在 Yii 1.1 的 bootstrap 中(app/index.php,第 13 行),我们使用 Yii::createWebApplication($config)->run();
命令启动应用程序,createWebApplication
方法是(framework/YiiBase.php, 第 96 行)
public static function createWebApplication($config=null)
{
return self::createApplication('CWebApplication',$config);
}
这样 createWebApplication
方法是(framework/YiiBase,第 123 行)
public static function createApplication($class,$config=null)
{
return new $class($config);
}
所以 createApplication('CWebApplication',$config)
解释成 new CWebApplication($config);
这样的东西,嗯?但是在 CWebApplication
class 上,我们没有任何构造函数! (framework/yiilite.php,第 1622 行)。
现在的问题是: WebApplication 如何获取 $config
变量的配置并适应它?
下面的函数将returnreturn new CWebApplication($config);
public static function createApplication($class,$config=null)
{
return new $class($config);
}
如果你检查 CWebApplication class 扩展 CApplication, CWebApplication.php
class CWebApplication extends CApplication
{
...
在 CApplication 中你会发现初始化的构造方法,framework/base/CApplication.php
abstract class CApplication extends CModule
{
....
public function __construct($config=null)
{
Yii::setApplication($this);
// set basePath at early as possible to avoid trouble
if(is_string($config))
$config=require($config);
if(isset($config['basePath']))
{
$this->setBasePath($config['basePath']);
unset($config['basePath']);
}
else
$this->setBasePath('protected');
Yii::setPathOfAlias('application',$this->getBasePath());
Yii::setPathOfAlias('webroot',dirname($_SERVER['SCRIPT_FILENAME']));
if(isset($config['extensionPath']))
{
$this->setExtensionPath($config['extensionPath']);
unset($config['extensionPath']);
}
else
Yii::setPathOfAlias('ext',$this->getBasePath().DIRECTORY_SEPARATOR.'extensions');
if(isset($config['aliases']))
{
$this->setAliases($config['aliases']);
unset($config['aliases']);
}
$this->preinit();
$this->initSystemHandlers();
$this->registerCoreComponents();
$this->configure($config);
$this->attachBehaviors($this->behaviors);
$this->preloadComponents();
$this->init();
}
abstract class CApplication
{
protected $config=array();
public function __construct($config=null) {
echo "CApplication::construct() called";
// DO anyting with config arrray which is from /protected/config/main.php
//$this->config = $config;
}
}
CWebApplication继承了CApplication所以也继承了Constructor方法。 要了解这个想法,您可以查看我下面的示例。
Edit
abstract class CApplication
{
protected $config=array();
public function __construct($config=null) {
echo "CApplication::construct() called";
// DO anyting with config arrray which is from /protected/config/main.php
//$this->config = $config;
}
}
class CWebApplication extends CApplication
{
public function test(){
echo "<pre>";
print_r($this->config);
echo "</pre>";
}
}
$config = array(
"name" =>"My Web Application",
"components" => array(
"db" => "connectionstring:..",
"urlManage"=> array(),
)
);
$obj = new CWebApplication($config);