Codeigniter在自定义库中设置常量

时间:2013-02-06 11:43:01

标签: php codeigniter

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

5 个答案:

答案 0 :(得分:2)

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

<?php
class my_class {
    const MY_CONSTANT = 10;

然后用:

来调用它
self::MY_CONSTANT

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

答案 1 :(得分:1)

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

 $this->load->library('library_name');
 echo library_name::MY_CONSTANT;

答案 2 :(得分:0)

通常,Codeigniter常量应位于config/constants.php内。然后我们只能访问整个应用程序中的所有常量。

答案 3 :(得分:0)

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

配置/ myconstants.php

在你的图书馆,

include(APPPATH。'config / myconstants.php');

答案 4 :(得分:0)

您应该在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

}