CodeIgniter Forums
Variables - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Variables (/thread-42808.html)



Variables - El Forum - 06-20-2011

[eluser]Perkin5[/eluser]
I have seen other posts relating to similar problems but none of them got an answer that works.

I am a beginner with codeigniter, using version 2.0.2 with php 5.3.

I am testing with a controller called start.php and the code is:

Code:
<?php  

class Start extends CI_Controller
{
    var $base;
    function _construct()
    {
        parent::_construct();
        $this->base = $this->config->item('base_url');
    }    
    function hello()
    {
        $data['base'] = $this->base;
        print_r ($data);
    }
    
    
}

When I go to the hello function in the browser, the print_r function shows that $data is empty (like this: Array ( [base] => ) ). Why is the value set for $this->base in the constructor not available in the hello method? I have now spent about 8 hours on this one issue without success.


Variables - El Forum - 06-20-2011

[eluser]danmontgomery[/eluser]
__construct, not _construct


Variables - El Forum - 06-20-2011

[eluser]Perkin5[/eluser]
I don't believe it! All those hours and it was that simple. All OK now!

I checked the User Guide yet again and under Controllers/Class Constructors there is no mention that it is a double underscore and it's not obvious to my 70 year old eyes.

Thank you so much, noctrum


Variables - El Forum - 06-20-2011

[eluser]danmontgomery[/eluser]
Constructors are covered more thoroughly on php.net Wink