Hi all,
I have presented my code below which causes error on my server. But i have tested the same code in some other server, it works fine.
If i load the validation library in the constructor then it’s working fine. But i needs to know why it has to be loaded in the constructor as i does not need the validation library in all the functions of the sample class.
I have also gone through the core files and found that there is function called “get_instance” with return the instance of the current controller (i.e., $this).
Instead of using like the following, which causes error
//Type 1
$this->validation->set_rules($rules);
Alternatively i have tried using like the following which works fine.
//Type 2
$this->get_instance()->validation->set_rules($rules);
To my knowledge $this && $this->get_instance() are the same.
If not, let me know the difference.
So may i know what causes error in the server. The same problem occurs while loading some models. Do i needs to check any php.ini settings ?
<?php
class Sample extends Controller
{
//Constructor
function Sample()
{
parent::Controller();
}
function index()
{
//Load the required libraries.
$this->load->library('validation');
//Set the validation rules
$this->_loginFrmRules();
}
function _loginFrmRules()
{
$rules['admin_name'] = 'trim|required';
//Type 1 - Causes Error
$this->validation->set_rules($rules);
//Type 2 - Working Fine
$this->get_instance()->validation->set_rules($rules);
$fields['admin_name'] = 'Username';
$this->validation->set_fields($fields);
}
}
?>
