EllisLab text mark
Advanced Search
     
Using Helper Functions Outside Controller
Posted: 10 November 2012 04:27 PM   [ Ignore ]
Joined: 2012-11-10
3 posts

Suppose I have a helper called “foobar” defined like this:

<?php if(!defined("BASEPATH")) exit("No direct script access allowed");

function 
getFoobar()
{
    
return "Foobar";
}

?> 

I add this helper to the list of automatically loaded helpers in config/autoload.php, and now in a controller, I want to do this:

<?php

$foobar 
getFoobar();

class 
Foobar extends CI_Controller
{
    
public function __construct()
    
{
        parent
::__construct();
    
}

    
public function index()
    
{
        
echo $foobar;
    
}
}

?> 

The problem is that this fails, saying getFoobar() isn’t defined (I assume this is because the loading of helpers and other things occurs when I call parent::__construct() in a controller).

How can I access helper functions outside of controllers?

 
Posted: 10 November 2012 04:45 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
3819 posts

Try

class Foobar extends CI_Controller
{
    
private $foobar;

    public function 
__construct()
    
{
        parent
::__construct();
        
$this->foobar getFoobar();
    
}

    
public function index()
    
{
        
echo $this->foobar;
    
}

If you created a MY_Controller (base controller) and put this in there, then it would be available to all of your controllers that extend MY_Controller.  But then, you also might just want to add the method to that instead of a helper, unless you are using it in other places like views or custom library, etc.

 Signature 
 
Posted: 10 November 2012 04:48 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
3819 posts

Here’s a good tutorial on what you can do with base controllers.
http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-base-Classes-Keeping-it-DRY
I highly recommend reading it as it really makes things simpler.

 Signature 
 
Posted: 10 November 2012 04:51 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Joined: 2012-11-10
3 posts

That is the obvious solution, but in this case I’m not sure it works.

The issue I’m having is that, in my particular case, getFoobar() returns a path to a PHP file I want to include, and doing require_once() inside of a class doesn’t really do what one expects (due to scoping).

 
Posted: 10 November 2012 04:57 PM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
3819 posts

Why don’t you have the helper instantiate the class and return the object instead of that way?

 Signature 
 
Posted: 10 November 2012 04:58 PM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
3819 posts

Or create a library and load that?

 Signature 
 
Posted: 10 November 2012 05:00 PM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Joined: 2012-11-10
3 posts

The object needs some data to be instantiated, which the helper doesn’t have reasonable access to.

As for making it a library, my understanding is that libraries typically are single-use - i.e., there will only ever be a single instance of a library, whereas my object lets me manage data, and I might have several instances of it in use at once.

 
Posted: 10 November 2012 05:06 PM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
3819 posts

The helper can access anything that CI has loaded or has access to.

$CI =& get_instance();
$data $CI->db->get('some_table')->result_array(); 

But with a library, you could just autoload it, bring CI into it (as above) and use whatever needed in the construct to get your stuff.  Then that library will be available to all of your controllers/views/other libraries/etc since its autoloaded.

class some_class {

  
private $CI;
  private 
$some_var NULL;
  function 
__construct()
  
{
    $this
->CI =& get_instance();
    
$this->some_var $this->CI->some_loaded_model->get_data();
  
}

  
function some_function()
  
{
    $this
->CI->load->view('some_view'$this->some_var);
  
}
 Signature