Red*_*Red 5 php codeigniter class
对于我目前的项目,我决定为一些常见的功能创建一个库.
例如: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;
}
}
?>
Run Code Online (Sandbox Code Playgroud)
我试图在我的控制器上加载它:
<?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());
}
}
?>
Run Code Online (Sandbox Code Playgroud)
我看不到任何错误...但我得到一个空白页面.
我怎么了?
谢谢 .
编辑:我收到此错误:
Call to a member function show_hello_world() on a non-object in C:\wamp\www\Project\application\controllers\admin.php on line 13
Run Code Online (Sandbox Code Playgroud)
Dam*_*rsy 15
我注意到一件事:parent::__construct()从你的库构造函数中删除它,因为它没有扩展任何东西所以没有父调用.
此外,通过在index.php中将环境设置为"development"来启用错误报告,您可能还希望在config/config.php中将日志记录阈值提高到4,以便记录错误.
试试这个简单的测试用例:
应用程序/库中的文件Pro.php:
class Pro {
function show_hello_world()
{
return 'Hello World';
}
}
Run Code Online (Sandbox Code Playgroud)
应用程序/控制器中的控制器admin.php
class Admin extends CI_Controller
{
function index()
{
$this->load->library('pro');
echo $this->pro->show_hello_world();
}
}
Run Code Online (Sandbox Code Playgroud)