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

[版本 3.x] controller里面load自定义的library,突然报错了

[复制链接]
发表于 2017-10-25 10:53:23 | 显示全部楼层 |阅读模式
之前用得好好的在,郁闷。
libraries/First.php

PHP复制代码
 
class First {
  public function __construct() {
    echo 111; //这里执行了的
  }
  public function test() {
      echo 'test';
  }
}
 
 
复制代码


controller里面
PHP复制代码
 
$this->load->library('first');
$this->first->test(); //一直报错,Call to a member function test() on null
 
 
复制代码


关键是以前都是对的,最近突然运行就报这个错,请教了。
发表于 2017-10-25 11:06:06 | 显示全部楼层
你是不是有定义了一个 first 的成员变量
 楼主| 发表于 2017-10-25 11:10:12 | 显示全部楼层
本帖最后由 michaelzhouh 于 2017-10-25 11:12 编辑

终于找到坑在哪里了。
类名我用的驼峰,我改成全小写就可以了。
比如上面那个类是First就可以,如果改成了FirstTest.php
PHP复制代码
 
$this->load->library('firstTest');
$this->firstTest->test();
 
复制代码

这样就会报错,必须要写成
PHP复制代码
 
$this->load->library('firstTest');
$this->firsttest->test();
 
复制代码

发表于 2017-10-27 18:19:20 | 显示全部楼层
自从用了CI我超级讨厌驼峰命名!
发表于 2017-10-27 18:29:28 | 显示全部楼层
gogogo1027 发表于 2017-10-27 18:19
自从用了CI我超级讨厌驼峰命名!

CI 的命名需要大写开头

本版积分规则