我想从 CI4 的本机
\Config\Constants.php
中获取用户定义的常量文件
我想写一个这样的类:
public class UserConstants{
const YEAR = 2024;
const MAX = 9999;
const FOO = "myfoo";
}
或
class UserConstantsStaticWay
{
private static $year;
private static $max;
private static $foo;
public static function getYear():int {return self::$year;}
public static function getMax():int {return self::$max;}
public static function getFoo():string {return self::$foo;}
}
这将允许我在我的控制器甚至我的视图中使用诸如
UserConstants::YEAR
或 UserConstantsStaticWay::getYear()
之类的语法。
在 CI4 中,我可以在哪里定义这样的类? 最优雅的方式是什么?在
Config
、Service
、Library
、简单的类定义或其他?
更重要的是,如何自动加载它并使其在所有控制器和视图中可用?那么不仅在
BaseController
定义中。
我在这里谈论的不是
CI4's Autoload PSR4
命名空间,而是旧的 Resources CI3 Autoloader
。
中设置全局常量
class UserConstants{
const YEAR = 2024;
const MAX = 9999;
const FOO = "myfoo";
}
define("UserConstants", UserConstants::class);
在应用程序中的任何位置使用
echo (new (UserConstants))::FOO;