Difference between index, construct, and class functions in codeigniter class

after working with Codeigniter, I still could not understand the difference between these three functions. all functions called automatically by calling a class?

class Upload extends Controller {

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

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

    function index()
    {
           echo 'test';
        }
}
+3
source share
2 answers

The Upload () function is a PHP4 thing. This is a constructor function for the Upload object, it is deprecated.

__ construct () is the "new" way to execute constructors

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

A visit / upload or / uploads / index will perform this function. The remaining two functions will always be performed.

Hope this clears up!

+6
source

Codeigniter Controllers.

, CI 2.0

( ​​ CI 2.0)

<?

class Upload extends CI_Controller
{

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

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

__construct()

index() - , , uri

. localhost/index.php/upload localhost/index.php/upload/index/

+2

Source: https://habr.com/ru/post/1793695/


All Articles