CodeIgniter Controller Constructor

前端 未结 4 1731
一整个雨季
一整个雨季 2021-02-07 10:34

I\'m very new to codeigniter , I wanted to know what is the meaning of a constructor in a controller . I saw the following code in a codeigniter tutorial -

cla         


        
4条回答
  •  北海茫月
    2021-02-07 11:07

    the constructor is magic Literally its called a magic method. what makes the constructor cool is that it will do things for you BEFORE any of the methods. So if you have an admin class, and someone should be logged in in order to access it - you can check for login in the constructor and bounce them out if they are not authorized.

    in the constructor you can load the models, libraries, helpers, etc that your class needs, and they will be available for any method in the class.

    you can load in variables that are used by methods. this is really useful for models.

提交回复
热议问题