I am working with CodeIgniter. In my "instancia" controller I need to create an instance from the controller "rubrica" to access a function from this.
instancia.php:
include_once APPPATH.'controllers/controller_ppal.php';
include_once(APPPATH.'controllers/rubrica.php');
class Instancia extends Controller_Ppal
{
function __construct()
{
parent::__construct();
$this->no_logged_msg = 'Debe estar autenticado en el sistema para acceder a esta opción.';
$this->no_auth_msg = 'No tiene autorización para acceder aquí.';
$this->no_partic = 'No ha agregado participantes a la lista aún.';
$this->datos_inc = 'No ha ingresado todos los datos de al menos uno de los participantes.';
$this->load->model('instancia_model');
$this->load->model('usuario_model');
}
...
function guardar()
{
$ext_class = new Rubrica();
$ext_class->session = $this->session;
$ext_class->load->model('usuario_model'); //In spite of it is loaded in "__construct" function
$id_rubrica = $ext_class->guardar();
}
}
rubrica.php:
include_once APPPATH.'controllers/controller_ppal.php';
class Rubrica extends Controller_Ppal {
function __construct()
{
parent::__construct();
$this->no_logged_msg = 'Debe estar autenticado en el sistema para acceder a esta opción.';
$this->no_auth_msg = 'No tiene autorización para acceder aquí.';
$this->load->model('rubrica_model');
$this->load->model('tag_model');
$this->load->model('usuario_model');
}
function guardar()
{
$_POST['usuarios'] = $this->usuario_model->todos_usuarios($this->session->userdata('id_usuario'));
}
}
I get the error message "Undefined property: Rubrica::$usuario_model". I have read several posts and I can't get the solution. Could someone help me?
If you have functions which needs to be shared between multiple controllers, you should write a library.
In application/libraries create Tools.php (or whatever you want)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Tools
{
protected $ci;
public function __construct()
{
$this->ci =& get_instance();
}
public function myfunction($data)
{
//If you want to use codeigniter's functionalities :
//use $this->ci->function()
}
}
And then in your controllers :
$this->load->library("tools");
$this->tools->myfunction($data);
If your library is meant to be used very often, you can load it once for all in config/autoload.php :
$autoload['libraries'] = array('database', 'tools', '...');