|
My_class.php 类的文件名:(放在 application/library/)
<?php
class My_class{
var $a='10000';
var $b='20000';
var $c='30000';
function My_class($config=array()){
$this->a =$config['a'];
$this->b =$config['b'];
$this->c =$config['c'];
}
function output(){
echo $this->a.'<br />'.$this->b.'<br />'.$this->c.'<br />';
}
}
?>
my_class.php 配置文件名:(放在 application/config/)
<?php
$config['a']='aaaaaa';
$config['b']='bbbbbb';
$config['c']='cccccc';
?>
我在控制器中这样调用:
function test(){
$this->config->load('my_class');
$this->load->library('my_class');
$this->my_class->output();
}
就会报一下错误:
A PHP Error was encountered
Severity: Notice
Message: Undefined index: a
Filename: libraries/My_class.php
Line Number: 7
配置文件中的参数没有办法传到类的构造函数中
但是类似这样传参可以:
$params = array('a' => 'large', 'b' => 'red','c'=>'blue');
$this->load->library('my_class',$params);
请各位CI高手指点 |
|