php 创建简单的 Codeigniter 库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8413940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create Simple Codeigniter library
提问by Red
For my current project i decided to create a library for some common functionalities.
对于我当前的项目,我决定为一些常见功能创建一个库。
Ex : Login_check,get_current_user etc.
例如:Login_check、get_current_user 等。
With my little knowledge i created a simple one but unfortunately its not working.
凭借我的一点知识,我创建了一个简单的,但不幸的是它不起作用。
Here my library :
这是我的图书馆:
FileName : Pro.php
and located in application/libraries
文件名: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;
}
}
?>
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.
我看不到那里有任何错误......但我得到了一个空白页。
Whats wrong with me ??
我怎么了??
Thank you .
谢谢你 。
Edit : I got this error :
编辑:我收到此错误:
Call to a member function show_hello_world() on a non-object in C:\wamp\www\Project\application\controllers\admin.php on line 13
回答by Damien Pirsy
One thing I notice: remove the parent::__construct()
from your library constructor, because it's not extending anything so has no parent to call.
我注意到一件事:parent::__construct()
从你的库构造函数中删除,因为它没有扩展任何东西,所以没有父级可以调用。
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.
此外,通过在 index.php 中将环境设置为“development”来启用错误报告,并且您可能还希望将 config/config.php 中的日志记录阈值提高到 4,以便记录错误。
Try this simple test-case:
试试这个简单的测试用例:
file Pro.php in application/libraries:
应用程序/库中的文件 Pro.php:
class Pro {
function show_hello_world()
{
return 'Hello World';
}
}
Controller admin.php in application/controllers
应用程序/控制器中的控制器 admin.php
class Admin extends CI_Controller
{
function index()
{
$this->load->library('pro');
echo $this->pro->show_hello_world();
}
}
回答by davidethell
while your class name is capitalized, all your references to the library when loading it and using it should be lower case. you also do not need the constructor, as the other commenter mentioned.
虽然您的类名是大写的,但在加载和使用它时对库的所有引用都应该是小写的。正如其他评论者提到的,您也不需要构造函数。
so instead of:
所以而不是:
echo($this->Pro->show_hello_world());
you should have:
你应该有:
echo($this->pro->show_hello_world());
回答by Daniel Lee
I prefer the standard php autoloader approach, with this you dont need to change your classes at all, you can use your standard classes without modifications
我更喜欢标准的 php 自动加载器方法,这样你根本不需要改变你的类,你可以使用你的标准类而无需修改
say for instance you class is class 'Custom_Example_Example2' and is stored in libraries in sub folders you can add this autoloader in the master index.php
比如说你的类是类'Custom_Example_Example2'并且存储在子文件夹的库中,你可以在主index.php中添加这个自动加载器
make sure it is added below the defined APPPATH constant
确保将其添加到定义的 APPPATH 常量下方
//autoload custom classes
function __autoload($className) {
if (strlen(strstr($className, 'Custom_')) > 0 ||
strlen(strstr($className, 'Other1_')) > 0 ||
strlen(strstr($className, 'Other2_')) > 0) {
$exp = explode('_', $className);
$file = APPPATH.'libraries';
if(!empty($exp)) {
foreach($exp as $segment) {
$file .= '/'.strtolower($segment);
}
}
$file .= '.php';
require_once $file;
//debug
//echo $file.'<br />';
}
}
This will look for class calls matching the 'Custom_' prefix and reroute them to the relative location in this case
在这种情况下,这将查找与“Custom_”前缀匹配的类调用,并将它们重新路由到相对位置
you only need to define the base prefix not the sub folders / classes these will be auto detected by this code
您只需要定义基本前缀而不是子文件夹/类,这些将被此代码自动检测
APPPATH.'libraries/custom/example/example2.php'
You can call it in the controller the standard php way
您可以在控制器中以标准的 php 方式调用它
$class = new Custom_Example_Example2;
or
或者
$class = new custom_example_example2();
You can modify the script to your liking currently it expects all folders and filenames in the library to be lowercase but you can remove the strtolower() function to allow multiple casing.
您可以根据自己的喜好修改脚本,目前它希望库中的所有文件夹和文件名都是小写的,但您可以删除 strtolower() 函数以允许多个大小写。
you can change the require once to echo to test the output by uncommenting this line and refresh the page, make sure you have a class init / test in the controller or model to run the test
您可以通过取消注释此行并刷新页面来更改 require 一次以回显以测试输出,确保您在控制器或模型中有一个类 init / test 来运行测试
echo $file.'<br />';
Thanks Daniel
谢谢丹尼尔
回答by Hitesh Gandhi
In Pro.php
在 Pro.php 中
class Pro{
protected $CI;
public function __construct() {
$this->CI = & get_instance();
}
public function showHelloWorld(){
return "Hello World";
}
}
In your controller
在您的控制器中
class Staff extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->database();
$this->load->helper(array('url_helper', 'url'));
$this->load->library("pro");
}
public function index() {
echo $this->pro->showHelloWorld();die;
}
}
Just do these things you can access your custom library in codeignitor.
只需执行这些操作,您就可以在 codeignitor 中访问您的自定义库。