Php 创建简单的Codeigniter库

Php 创建简单的Codeigniter库,php,class,codeigniter,Php,Class,Codeigniter,对于我当前的项目,我决定为一些常见功能创建一个库 例如:登录检查、获取当前用户等 用我的一点知识,我创造了一个简单的,但不幸的是它不工作 这里是我的图书馆: 文件名:Pro.php并位于应用程序/库中 class Pro{ public function __construct() { parent::_construct(); $CI =& get_instance(); $CI->load->helper('

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

例如:登录检查、获取当前用户等

用我的一点知识,我创造了一个简单的,但不幸的是它不工作

这里是我的图书馆:

文件名:
Pro.php
并位于
应用程序/库中
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;
    }
}

?> 
我试着把它加载到我的控制器上:

<?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());
    }
}

?>

我注意到一件事:从库构造函数中删除
父项::\u construct()
,因为它没有扩展任何内容,因此没有可调用的父项

另外,通过在index.php中将环境设置为“development”来启用错误报告,您可能还希望在config/config.php中将日志记录阈值提高到4,以便记录错误

尝试以下简单的测试用例:

应用程序/库中的Pro.php文件:

class Pro {

  function show_hello_world()
  {
    return 'Hello World';
  }
}
应用程序/控制器中的Controller admin.php

class Admin extends CI_Controller
{
    function index()
    {
        $this->load->library('pro');
        echo $this->pro->show_hello_world();
    }
}

虽然类名是大写的,但在加载和使用类库时对它的所有引用都应该是小写的。正如另一位评论者所提到的,您也不需要构造函数

因此,不是:

echo($this->Pro->show_hello_world());
你应该:

echo($this->pro->show_hello_world());

我更喜欢标准的php自动加载方法,使用这种方法,您根本不需要更改您的类,您可以使用您的标准类而无需修改

例如,假设您的类是“Custom\u Example\u Example2”类,并且存储在库中 在子文件夹中,您可以在master index.php中添加此自动加载器

确保将其添加到定义的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 />';
}
}
您可以用标准的php方式在控制器中调用它

$class = new Custom_Example_Example2;

您可以根据自己的喜好修改脚本。当前,它希望库中的所有文件夹和文件名都是小写的,但您可以删除strtolower()函数以允许多个大小写

您可以通过取消对此行的注释并刷新页面来更改require once to echo以测试输出,确保控制器或模型中有类init/test来运行测试

echo $file.'<br />';
echo$文件。“
”;
谢谢 Daniel在Pro.php中

class Pro{
    protected $CI;
    public function __construct() {
        $this->CI = & get_instance();
    }

    public function showHelloWorld(){
        return "Hello World";
    }
}
在控制器中

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;

    }
}

只需执行这些操作,您就可以在codeignitor中访问自定义库。

谢谢,但是现在
未定义属性:Admin::$Pro
您需要从库构造函数中删除它,而不是从控制器contstructor
define('ENVIRONMENT','development')
因此其正确的…
log_threshold
设置为4..\Damien我已将其从库构造函数中删除。请尝试至少减少示例代码,例如删除不必要的库调用。只要装上你的和see@Hary没有错误,我得到一个空白页。在C:\wamp\www\project\application\controllers\admin.php的非对象上调用成员函数show\u hello\u world()13@Hary
echo($this->Pro->show_hello_world())@DileepDil您在我的答案中完全尝试了整个测试用例吗?(我更新了一点)@DamienPirsy是的,我下载了…现在我正在下载codeigniter[新版本]谢谢你,事实上我弄错了,但你回答得很好。@DileepDil我知道你没有按照我告诉你的做,因为如果你仔细阅读我的答案,而不是仅仅应用一点这个或那个,你会发现我一直在使用与这个答案完全相同的东西。哦,好吧,非常正确,@DamienPirsy在我之前得到了这个。我没有看到,因为他没有明确提到,但你可以在他的回答中看到小写。@davidethell不要误解我,我不想剥夺你被接受的答案,我不在乎:),当有人说他在做某事,而不是按照他的方式做时,我只会感到不安;我已经看过很多了。Dileep,将此作为公认的答案,下次注意那些花了30分钟试图找出代码错误的人;你不必把它当作已接受的东西删除,我不在乎,别误会我。如果您接受自动完成,IDE通常会改变这一点。不管怎样,很高兴你解决了问题,干杯
echo $file.'<br />';
class Pro{
    protected $CI;
    public function __construct() {
        $this->CI = & get_instance();
    }

    public function showHelloWorld(){
        return "Hello World";
    }
}
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;

    }
}