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

[控制器] 自定义类库的问题

[复制链接]
发表于 2012-11-7 23:02:19 | 显示全部楼层 |阅读模式
自定义类:
PHP复制代码
 
<?php
 
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
 
class Useridentify {
 
    var $p;
 
    public function __construct($p ='') {
        // Do something with $params
        echo $p;
    }
}
 
?>
 
复制代码

控制器:
PHP复制代码
 
<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
class Home extends CI_Controller {
    public function index() {
        $a = "hello";
        $this->load->library('useridentify',$a);
    }
}
 
复制代码


为什么构造函数中不能输出参数p?

发表于 2012-11-9 13:36:45 | 显示全部楼层
本帖最后由 bluelomo 于 2012-11-9 13:39 编辑

请见文档:http://codeigniter.org.cn/user_guide/general/creating_libraries.html

如下图所示,“当初始化类库时,你可以通过第二个参数动态的传递数组到类的构造函数中去”,换言之,如果你要传参数,你必须且只能使用一个动态数组格式,使用其他格式一律无效。

2.JPG
发表于 2013-11-30 09:22:12 | 显示全部楼层
在自定义类库中 使用
public function __construct(){
                parent::__construct();
                $CI =& get_instance();
                $CI->load->helper('url');
                $CI->config->item('base_url');
        }

报错 :Fatal error: Cannot access parent:: when current class scope has no parent in E:\APMServ5.2.6\www\htdocs\application\libraries\Sys_message.php on line 5


是怎么回事呢

点评

因为没有父类啊..  发表于 2014-2-11 11:54
发表于 2014-2-8 16:27:29 | 显示全部楼层
楼上的,你确定你继承了一个父类?
发表于 2014-5-4 17:10:48 | 显示全部楼层
楼主自定义类木有调用到吧
   parent::__construct();
   $CI =& get_instance();
   $CI ->load->library('useridentify',$a);
这样试试!
发表于 2014-5-13 15:03:46 | 显示全部楼层
传递变量并且要放了application/libraries下面

本版积分规则