php 如何在constants.php中的codeigniter中定义全局常量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13990926/
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
how to define global constants in codeigniter in constants.php
提问by Nishant Lad
I have stored my global constants in config->constants.php but it creating problem and throwing error I have defined it like this, how to call it in controller, code
我已经将我的全局常量存储在 config->constants.php 中,但它会产生问题并抛出错误我已经这样定义了它,如何在控制器中调用它, 代码
define('SuperAdmin','1');
define('Admin','2');
define('CityAdmin' '3');
define('SupportAdmin','4');
define('RestaurantUser','5');
define('FrontUser','6');
define('Other','7');
回答by Rick Calder
You don't need to load constants anywhere, they're automatically autoloaded by CI itself, that is the point of them. To use them keep in mind they are constants and not a variable as you're used to with the $. So in your case after defining them, anywhere in your application you could for example use:
您不需要在任何地方加载常量,它们由 CI 本身自动加载,这就是它们的重点。要使用它们,请记住它们是常量,而不是您习惯使用 $ 的变量。因此,在您定义它们之后的情况下,您可以在应用程序中的任何位置使用,例如:
if($user->userType == SuperAdmin)
{
do something here;
}
Note that the SuperAdmin is written without a $ preceding it and not inside quotes. Again, after defining them in the constants file you need to do nothing else to begin using them in your application.
请注意,SuperAdmin 的编写没有在它前面加上 $ 并且不在引号内。同样,在常量文件中定义它们之后,您无需执行任何其他操作即可开始在您的应用程序中使用它们。
回答by NomanJaved
First of all use the conventions that you must define the constants in capital letters. Like
首先使用必须以大写字母定义常量的约定。喜欢
define('SUPERADMIN','1');
define('ADMIN','2');
define('CITYADMIN' '3');
you can use the constants in controller like
您可以在控制器中使用常量,例如
if($user->userType == SUPERADMIN)
{
//Your code ...
}
and if you want to use in html file/ view file in codeigniter you can use constants in html file like
如果您想在 codeigniter 中的 html 文件/视图文件中使用,您可以在 html 文件中使用常量,例如
<?php echo SUPERADMIN; ?>
You can access the constants in this way.
您可以通过这种方式访问常量。
回答by Kishor Gavande
Always follow naming convention for constant in only capital letter. If you declared constant in application/config/constants.php file then it will available through out your application.
始终遵循常量的命名约定,仅使用大写字母。如果您在 application/config/constants.php 文件中声明了常量,那么它将在您的整个应用程序中可用。
回答by Deep123
This is a correct way to define global constants in config->constants.php. Can you provids the detail about the error that your are facing and code where your are trying to use these variables.Keep in mind that these variables not need $ sign to use. Like it can be used as $a = SuperAdmin not as $a = $SuperAdmin
这是在 config->constants.php 中定义全局常量的正确方法。您能否提供有关您面临的错误的详细信息以及您尝试使用这些变量的代码。请记住,这些变量不需要使用 $ 符号。就像它可以用作 $a = SuperAdmin 而不是 $a = $SuperAdmin
回答by Khandad Niazi
You may utilise config file (system/application/config/config.php) to set configuration related variables.
Or use constant file (system/application/config/constants.php) to store site preference constants.

