3

我正在为一个项目使用 codeigniter。我有一个自定义库,其中有一些常量,我将在库中的自定义函数中使用这些常量。我如何声明这样的常量?

4

5 回答 5

3

如果您打算仅在您的库中使用该常量,则必须在您的类中声明它:

<?php
class my_class {
    const MY_CONSTANT = 10;

然后调用它:

self::MY_CONSTANT

如果你需要你的常量在应用程序的其余部分可用,你必须在你的application/config/constants.php文件中声明它

于 2013-02-06T11:47:28.693 回答
1

以这种方式访问​​库中的常量。有用。

 $this->load->library('library_name');
 echo library_name::MY_CONSTANT;
于 2014-03-04T11:09:22.033 回答
1

config/constants.php您应该按如下方式声明常量:

class MyConstantsClass {

    public static function get_timezones_list() {
        return array(
            "-12" => "(GMT -12:00) Eniwetok, Kwajalein",
            "-11" => "(GMT -11:00) Midway Island, Samoa",
            "-10" => "(GMT -10:00) Hawaii",
            "-9" => "(GMT -9:00) Alaska",
            "-8" => "(GMT -8:00) Pacific Time (US &amp; Canada)",
            "-7" => "(GMT -7:00) Mountain Time (US &amp; Canada)",
            "-6" => "(GMT -6:00) Central Time (US &amp; Canada), Mexico City",
            "-5" => "(GMT -5:00) Eastern Time (US &amp; Canada), Bogota, Lima",
            "-4" => "(GMT -4:00) Atlantic Time (Canada), Caracas, La Paz",
            "-3.5" => "(GMT -3:30) Newfoundland",
            "-3" => "(GMT -3:00) Brazil, Buenos Aires, Georgetown",
            "-2" => "(GMT -2:00) Mid-Atlantic",
            "-1" => "(GMT -1:00) Azores, Cape Verde Islands",
            "0" => "(GMT) Western Europe Time, London, Lisbon, Casablanca",
            "1" => "(GMT +1:00) Brussels, Copenhagen, Madrid, Paris",
            "2" => "(GMT +2:00) Kaliningrad, South Africa",
            "3" => "(GMT +3:00) Baghdad, Riyadh, Moscow, St. Petersburg",
            "3.5" => "(GMT +3:30) Tehran",
            "4" => "(GMT +4:00) Abu Dhabi, Muscat, Baku, Tbilisi",
            "4.5" => "(GMT +4:30) Kabul",
            "5" => "(GMT +5:00) Ekaterinburg, Islamabad, Karachi, Tashkent",
            "5.5" => "(GMT +5:30) Bombay, Calcutta, Madras, New Delhi",
            "6" => "(GMT +6:00) Almaty, Dhaka, Colombo",
            "7" => "(GMT +7:00) Bangkok, Hanoi, Jakarta",
            "8" => "(GMT +8:00) Beijing, Perth, Singapore, Hong Kong",
            "9" => "(GMT +9:00) Tokyo, Seoul, Osaka, Sapporo, Yakutsk",
            "9.5" => "(GMT +9:30) Adelaide, Darwin",
            "10" => "(GMT +10:00) Eastern Australia, Guam, Vladivostok",
            "11" => "(GMT +11:00) Magadan, Solomon Islands, New Caledonia",
            "12" => "(GMT +12:00) Auckland, Wellington, Fiji, Kamchatka"
        );
    }   

    public static function get_codes() {        
        return array();
    }

   // .... and so on....

}

然后在您的自定义库中:

class MyLibraryClass {

    private $timezones;

    public function __construct(){
        $this->CI =& get_instance(); //in case you need it
        $this->timezones = MyConstantsClass::get_timezones_list();
    }

    //do whatever you want with your constants
    echo $current_timezone = $this->timezones['-5']; //print (GMT -5:00) Eastern Time (US &amp; Canada), Bogota, Lima

}
于 2017-05-19T15:21:10.717 回答
0

通常 Codeigniter 常量应该在里面config/constants.php。然后只有我们可以访问整个应用程序中的所有常量。

于 2013-02-06T11:50:05.287 回答
0

您可以创建自己的常量文件并将其包含在您的库中:

配置/myconstants.php

在你的图书馆里,

包括(APPPATH。'config/myconstants.php');

于 2013-02-06T11:54:37.567 回答