xcnuser 发表于 2012-11-7 23:02:19

自定义类库的问题

自定义类:

<?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
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?

bluelomo 发表于 2012-11-9 13:36:45

本帖最后由 bluelomo 于 2012-11-9 13:39 编辑

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

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

蒲哥 发表于 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


是怎么回事呢

sunboat 发表于 2014-2-8 16:27:29

楼上的,你确定你继承了一个父类?

smilecc123 发表于 2014-5-4 17:10:48

楼主自定义类木有调用到吧
   parent::__construct();
   $CI =& get_instance();
   $CI ->load->library('useridentify',$a);
这样试试!

feimengv 发表于 2014-5-13 15:03:46

传递变量并且要放了application/libraries下面
页: [1]
查看完整版本: 自定义类库的问题