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

例如:Login_check,get_current_user等。

据我所知,我创建了一个简单的程序,但不幸的是它没有用。

这是我的图书馆:

FileName: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中将环境设置为“development”来启用错误报告,并且您可能还希望将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/

10-12 17:51
查看更多