Php Config Yii::不带构造函数的WebApplication
在yii1.1(app/index.php,第13行)的引导中,我们使用Php Config Yii::不带构造函数的WebApplication,php,oop,yii,Php,Oop,Yii,在yii1.1(app/index.php,第13行)的引导中,我们使用Yii::createWebApplication($config)->run()启动应用程序命令和createWebApplication方法是(framework/YiiBase.php,第96行) 这样createWebApplication方法是(framework/YiiBase,第123行) 所以createApplication('CWebApplication',$config)解释为类似newcWebApp
Yii::createWebApplication($config)->run()启动应用程序
命令和createWebApplication
方法是(framework/YiiBase.php,第96行)
这样createWebApplication
方法是(framework/YiiBase,第123行)
所以
createApplication('CWebApplication',$config)
解释为类似newcWebApplication($config)的东西代码>,哼?但是在CWebApplication
类上,我们没有任何构造函数!(framework/yiilite.php,第1622行)
现在的问题是:Web应用程序如何在$config
变量上获得配置并使自己适应它?下面的函数将返回返回新的CWebApplication($config)代码>
如果选中CWebApplication类extend CapApplication,
CWebApplication.php
class CWebApplication extends CApplication
{
...
在capapplication中,您将找到初始化的构造函数方法,framework/base/capapplication.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继承CAPApplication,因此它也继承构造函数方法。
为了得到这个想法,你可以检查我下面的例子
编辑
抽象类capapplication
{
受保护的$config=array();
公共函数构造($config=null){
echo“capapplication::construct()调用”;
//使用/protected/config/main.php中的config array执行任何操作
//$this->config=$config;
}
}
类CWebApplication扩展了CAPApplication
{
公共功能测试(){
回声“;
打印($this->config);
回声“;
}
}
$config=array(
“名称”=>“我的Web应用程序”,
“组件”=>阵列(
“db”=>“连接字符串:…”,
“urlmanager”=>array(),
)
);
$obj=新的CWebApplication($config);
但是在CWebApplication
上,我们不调用父级的构造函数。对于调用capapplication
constructor,我们必须有类似于parent::u construct
的代码,对吗?谢谢Yatin,现在我了解了一个新东西:-)。
public static function createApplication($class,$config=null)
{
return new $class($config);
}
class CWebApplication extends CApplication
{
...
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;
}
}
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);