在代码点火器中调用helper和library的方法有什么区别吗?

Nir*_*paz 11 php codeigniter codeigniter-2 codeigniter-helpers

我有点困惑,在代码点火器中使用库和助手的方法.我还在学习代码点火器.

CONTROLLER

function index(){
    $this->load->helper('text');
    $this->load->library('auth'); //custom library

    $data['string'] = 'this is sample ..... this is sample';
    $this->load->view('article', $data);
}
Run Code Online (Sandbox Code Playgroud)

视图

<?php 
if(is_logged_in()){    //is_logged_in() is the method from the library, 'auth'
    echo 'You are logged in';
}
<p><?php echo word_limiter($string, 10); ?></p> <!--word_limiter() is the method from the helper, 'text' -->
Run Code Online (Sandbox Code Playgroud)

在上面的视图文件中,帮助方法word_limiter()工作正常.但该方法is_logged_in()不起作用.但如果我这样做($this->auth->is_logged_in()),它会起作用.

但是为什么来自helper的方法word_limiter()不必像这样写($this->text->word_limiter()).

调用helper和library的方法有什么区别吗?

Has*_*ami 27

CodeIgniter帮助程序是一组相关的函数(公共函数),您可以在模型,视图,控制器,...中使用它们.

加载(包含)该文件后,您可以访问这些功能.

但是库是一个类,您需要创建类的实例(by $this->load->library()).并且您需要使用该对象$this->...来调用方法.

作为一个拇指规则:一个库用于面向对象的上下文(Controller,...),而一个帮助器更适合在视图中使用(非面向对象).