CodeIgniter Controller Constructor

前端 未结 4 1734
一整个雨季
一整个雨季 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:11

    Well, that's a more general PHP question. Anyway, yes, the magic method __construct() is called (automatically) upon each instantiation of the class, as you can see in the manual: http://www.php.net/manual/en/language.oop5.decon.php

    Usually, in CI is not necessary to call a constructor, unless you actually want one. In the example you posted, the code loads the helper on every instantiation of the class - which is the same as loading the helper in every method, just saves a lot of typing and ensures it's not forgotten. You can alternatively put the library/helper/model you want to have alywas loaded in the respective autoload array in config/autoload.php (check "autoloading" in CI's manual)

    Once you define a constructor in your child Controller you're compelled to call the parent constructor (of the mail CI_Controller class), because there is where the main CI object is created and all the classes are loaded, and you need those in your child controller too; if fail to do so your child class will construct separately and won't inherit.

    I hope I made myself clear, english is not my mothertongue :)

提交回复
热议问题