3

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';
        }
}

2 Answers 2

6

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!

2

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/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.