创建简单的Codeigniter库 [英] Create Simple Codeigniter library
问题描述
对于我当前的项目,我决定为一些常见的功能创建一个库。
For my current project i decided to create a library for some common functionalities.
例如:Login_check,get_current_user等。
Ex : Login_check,get_current_user etc.
我的小知识我创建了一个简单的,但不幸的是它不工作。
With my little knowledge i created a simple one but unfortunately its not working.
这里我的图书馆:
FileName: Pro.php
并位于应用程式/ libraries
FileName : Pro.php
and located in 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;
}
}
?>
我试图将其加载到我的控制器上:
And i tried to load it on my 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());
}
}
?>
我看不到任何错误...但我得到一个空白页。
I cant see any erros there...but i am getting a blank page.
我的错误
谢谢。
编辑:我收到此错误:
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()
,因为它没有扩展任何东西,
One thing I notice: remove the parent::__construct()
from your library constructor, because it's not extending anything so has no parent to call.
此外,通过在index.php中将环境设置为development来启用错误报告,您还可能希望将日志阈值提高到4, config.php,以便您记录错误。
Also, enable error reporting by setting the environment to "development" in index.php, and you might also want to raise the logging threshold to 4 in config/config.php so you log errors.
尝试这个简单的测试用例:
Try this simple test-case:
文件Pro.php应用程式/程式库:
file Pro.php in application/libraries:
class Pro {
function show_hello_world()
{
return 'Hello World';
}
}
application / controllers中的控制器admin.php
Controller admin.php in application/controllers
class Admin extends CI_Controller
{
function index()
{
$this->load->library('pro');
echo $this->pro->show_hello_world();
}
}
这篇关于创建简单的Codeigniter库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!