CodeIgniter Forums

Full Version: Loading helpers/librarys in _construct
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Braden Schaeffer[/eluser]
I'm not sure I understand how the _construct function works in CI.

What seems like a straight forward way to load helpers and librarys is giving me fits. I've searched the forums and googled the hell out of this, but I am still not clear on what to do. I also understand that I can auto load helpers and librarys, but I'm not sure that's really what I want. Stacking your auto_load has got to be the wrong way to go.

Here's some quick example code:

Code:
class Edit extends Controller {

    function __construct()
    {
        parent::Controller();

        $this->load->helper('form');
        $this->load->library('form_validation');
    }
    
    function myForm()
    {
        $this->form_validation->set_rules('input', 'input', 'required');

        //More code here...
    }

}

And I'm getting this non-object error when I call the form_validation helper afterwords:

Quote:Fatal error: Call to a member function on a non-object in /nfs/c05/h01/mnt/70148/domains/themesaur.us/html/application/controllers/edit.php on line 12

I'm sure loading helpers in the _construct function is possible. It has to be. Or... maybe it's not possible.

Someone has to know how to do this.

Thanks in advance!!

El Forum

[eluser]Dam1an[/eluser]
Are you running PHP5? The __construct method is PHP5 only, and if you're using PHP4, your constructor is a function with the same name as the class (with the first character upper case), so you'd call a function Edit in this case and put the loads in there

El Forum

[eluser]brianw1975[/eluser]
as an addendum: PHP4 will not throw an error or notification or anything if you have __construct() it will completely ignore it and you will often end up scratching your head going "The class works, but why isn't this tiny bit of code working" or "Why does it say that [something] isn't an object, I clearly made it an object in the constructor".

El Forum

[eluser]Braden Schaeffer[/eluser]
Thanks for the quick replies.

Just as a reference, I'm running PHP version 5.2.6-1 hosted on Media Temple.

El Forum

[eluser]brianw1975[/eluser]
libraries can be loaded in the constructor, thats for sure.

I'll admit that I haven't used the form_validation helper yet, but part of me is thinking (unless you just bodged together some sample code) that a problem may be popping up because of incorrectly supplied data (namely your rules) to set_rules().

According to the user guide http://ellislab.com/codeigniter/user-gui...ation.html the rules should be supplied as an array with the field names associated with their necessary rule.

Perhaps you can supply corrected code or provide more detailed code?

El Forum

[eluser]Braden Schaeffer[/eluser]
[quote author="brianw1975" date="1251156901"]I'll admit that I haven't used the form_validation helper yet, but part of me is thinking (unless you just bodged together some sample code) that a problem may be popping up because of incorrectly supplied data (namely your rules) to set_rules().[/quote]

I just created an example page to reproduce the problem. I'll admit I had much more code than I posted previously, but here is exactly what the markup for the example controller looks like, and it reproduces the same error message:

Code:
class Example extends Controller {
    
    function _construct()
    {
        parent::Controller();
        $this->load->helper('form');
        $this->load->library('form_validation');
    }
    
    function index()
    {
        $this->form_validation->set_rules('name', 'name', 'max_length[100]|xss_clean');
        
        if($this->form_validation->run() == FALSE)
        {
            $data['content'][0] = "Bad Submission";
        }
        else
        {
            $data['content'][0] = "Congratulations";
        }
        
        $data['content'][1] = $this->load->view('some/form', '', TRUE);
        
        $this->load->view('standard/header');
        $this->load->view('standard/content', $data);
        $this->load->view('standard/footer');
    }
    
}

A few things...

(1) I know that I am passing a valid rule string because it works if I load the helper and library in the index function.

(2) The view 'some/form' doesn't even exist, but the script dies before it even realizes this. I have also tried it with a simple form in a view that does exist, but it doesn't seem to matter.

I really appreciate all you're replies. Whether we find an answer or not, you guys are awesome. Thanks!

El Forum

[eluser]tomcode[/eluser]
function _construct() should be function __construct()

El Forum

[eluser]Braden Schaeffer[/eluser]
I think I had it that way before, but I changed it. No dice!!!

I might just incorporate a setup function. It's almost as good as __construct()

El Forum

[eluser]tomcode[/eluser]
1. Check whether the __construct() function is executed
2. Rename __construct into example()
3. print out phpversion()

El Forum

[eluser]Braden Schaeffer[/eluser]
I'm! A! Jackass!

I didn't realize you set your PHP version on a per domain basis in Media Temple (maybe you do it in most hosting services, I don't know). I went to my main domain to echo out the version number becuase I had a file setup already to do that, not the sub domain I was using to test the app.

@tomcode I finally realized it when I echoed out the version number in the app itself.

Sorry for wasting everyone's time on such an moron move.

Thanks for all your help.