对于我当前的项目,我决定为一些常用功能创建一个库。
例如: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/