php CodeIgniter 控制器构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13555822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:40:35  来源:igfitidea点击:

CodeIgniter Controller Constructor

phpcodeigniter

提问by Joel_Blum

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 -

我对 codeigniter 很陌生,我想知道控制器中构造函数的含义是什么。我在 codeigniter 教程中看到了以下代码 -

class upload extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper(form);
    }

    // rest of the class...

My question is when is the constructor invoked - is it called every time the controller serves a request (e.g the controller class is instantiated for each request it receives?)

我的问题是何时调用构造函数 - 每次控制器服务请求时都会调用它(例如,控制器类为它收到的每个请求实例化?)

采纳答案by Damien Pirsy

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

嗯,这是一个更一般的 PHP 问题。无论如何,是的,在每次实例化类时都会(自动)调用魔术方法 __construct(),如您在手册中所见: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)

通常,在 CI 中没有必要调用构造函数,除非你真的想要一个。在您发布的示例中,代码在类的每个实例化时加载助手 - 这与在每个方法中加载助手相同,只是节省了大量输入并确保不会忘记它。您也可以将要加载的库/助手/模型放在 config/autoload.php 中的相应自动加载数组中(检查 CI 手册中的“自动加载”)

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.

一旦你在你的子控制器中定义了一个构造函数,你就不得不调用父构造函数(邮件 CI_Controller 类),因为在那里创建了主 CI 对象并加载了所有类,而你的孩子需要这些控制器也是;如果不这样做,您的子类将单独构造并且不会继承。

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

我希望我说清楚,英语不是我的母语:)

回答by cartalot

the constructor is magicLiterally 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.

构造函数是魔术从字面上看,它被称为魔术方法。让构造函数很酷的是它会在任何方法之前为你做一些事情。因此,如果您有一个 admin 类,并且应该有人登录才能访问它 - 您可以在构造函数中检查登录,如果他们未经授权,则将其退回。

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.

您可以加载方法使用的变量。这对模型非常有用。

回答by Mian Usman

Don't use _construct()function in latest apache & codeigniter

不要_construct()最新的 apache 和 codeigniter 中使用函数

Use helperlin in index()function

使用helperlin in index()功能