怎样用配置文件给自己写的类传参???
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高手指点 在你的library里加载config
http://codeigniter.org.cn/user_guide/libraries/config.html (配置文件名最好不要和類名起得一樣)
$this->config->load('my_config',true);
$params = $this->config->item('my_config'); //這個地方最好把$params打印出來看看
$this->load->library('my_class',$params);
試一下吧
页:
[1]