用户
 找回密码
 入住 CI 中国社区
搜索
查看: 3218|回复: 2
收起左侧

[已解决] 怎样用配置文件给自己写的类传参???

[复制链接]
发表于 2010-3-24 16:14:57 | 显示全部楼层 |阅读模式
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高手指点
发表于 2010-3-24 16:31:57 | 显示全部楼层
发表于 2010-3-25 01:37:46 | 显示全部楼层
(配置文件名最好不要和類名起得一樣)
$this->config->load('my_config',true);
$params = $this->config->item('my_config'); //  這個地方最好把$params打印出來看看
$this->load->library('my_class',$params);
試一下吧

本版积分规则