开发者

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 aut开发者_C百科omatically by calling the class?

class Upload extends Controller {

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

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

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


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!


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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜