Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between index, construct and class name functions in codeigniter class

after working with Codeigniter I still couldn't figure out the difference between these 3 functions. does all the functions called automatically by calling the class?

class Upload extends Controller {

    function Upload()
    {
       parent::Controller();
           echo 'test';
        }

        function  __construct()
        {
           parent::Controller();
           echo 'test';
        }

    function index()
    {
           echo 'test';
        }
}
like image 210
Amir Noorani Avatar asked Jun 04 '26 23:06

Amir Noorani


2 Answers

function Upload() is a PHP4 thing. That is the constructor function for the Upload object, this is deprecated.

__construct() is the 'new' way of doing constructors

index() gets called on the index action, which is the default action

Visiting /uploads or /uploads/index will run this function. The other two functions will always run.

Hope this clears it up!

like image 192
James Hall Avatar answered Jun 06 '26 14:06

James Hall


You really need to start over with a blank screen and read through the documentation on Codeigniter Controllers.

and make sure you are using CI 2.0

edited version (corrected for CI 2.0)

<?

class Upload extends CI_Controller
{

    function  __construct()
    {
       parent::__construct();
       echo 'test';
    }

    function index() 
    {
       echo 'test';
    }
}

__construct() gets called every time the controller is loaded

index() is the default function that is called if no function is given in the uri

ex. localhost/index.php/upload will actually call localhost/index.php/upload/index/

like image 34
jondavidjohn Avatar answered Jun 06 '26 14:06

jondavidjohn