php - 创建简单的 Codeigniter 库

标签 php class codeigniter

对于我当前的项目,我决定为一些常用功能创建一个库。

例如:Login_check、get_current_user 等

凭借我的一点知识,我创建了一个简单的,但不幸的是它不起作用。

这是我的图书馆:

文件名:Pro.php 位于application/libraries

class Pro{

    public function __construct()
    {

       parent::_construct();
        $CI =& get_instance();
       $CI->load->helper('url');
       $CI->load->library('session');
       $CI->load->database();
    }

    function show_hello_world()
    {
        $text = "Hello World";
        return $text;
    }
}

?> 

然后我尝试将它加载到我的 Controller 上:

<?php
class Admin extends CI_Controller
{
    function __construct()
    {     
        parent::__construct();
        $this->load->database();
        $this->load->library(array('session'));
        $this->load->library("Pro");
    }
    function index()
    {
        echo($this->Pro->show_hello_world());
    }
}

?>

我在那里看不到任何错误...但我得到的是空白页。

我怎么了??

谢谢。

编辑:我收到这个错误:

Call to a member function show_hello_world() on a non-object in C:\wamp\www\Project\application\controllers\admin.php on line 13

最佳答案

我注意到一件事:从你的库构造函数中删除 parent::__construct(),因为它没有扩展任何东西,所以没有父级可以调用。

此外,通过在 index.php 中将环境设置为“开发”来启用错误报告,您可能还想在 config/config.php 中将日志记录阈值提高到 4,以便记录错误。

试试这个简单的测试用例:

在应用程序/库中文件 Pro.php:

class Pro {

  function show_hello_world()
  {
    return 'Hello World';
  }
}

应用程序/ Controller 中的 Controller admin.php

class Admin extends CI_Controller
{
    function index()
    {
        $this->load->library('pro');
        echo $this->pro->show_hello_world();
    }
}

关于php - 创建简单的 Codeigniter 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8413940/

相关文章:

php - 使用存储过程将值从 Controller 传递到模型 CodeIgniter

javascript - 通过javascript向每个DIV添加类而不覆盖现有类?

php - 创建一个 loadmore 按钮并在同一 codeigniter View 中显示下一组内容

php - Native-session 针对代码点火器 2.1.4 进行了修改...这有意义吗?

php mysql下拉问题

PHP从mysql打印在线用户名

swift - 为什么我们不能在闭包定义中使用实例成员?

javascript - 如何使用 eclipse-mars IDE 将 jquery 插件设置到我的 codeigniter 项目中

php - PHP 有没有像 Python 的模板字符串那样的特性?

python - 在 Python 中从同一文件调用类函数