[英]PHP constant in static variable
I need to use PHP constant in a static variable but I discover today that it's not possible : Is a global PHP CONSTANT available inside of a Class file? 我需要在静态变量中使用PHP常量,但今天发现这是不可能的: 在类文件中可以使用全局PHP CONSTANT吗?
define("TABLE_PREFIX", "TEST_");
class Test {
private static $sql_query = "select * from ".TABLE_PREFIX."USER";
public static function show_query1() {
echo "My first test";
echo self::$sql_query;
}
public static function show_query2() {
echo "My second test";
echo self::$sql_query;
}
}
Test::show_query1();
Test::show_query2();
I don't want to pass the constant as an argument to the static function and I don't want to declare $sql_query in each static function. 我不想将常量作为参数传递给静态函数,也不想在每个静态函数中声明$ sql_query。
What is the best way to to it ? 最好的方法是什么?
EDIT : add demo = http://codepad.org/aqzj2TJh 编辑:添加演示= http://codepad.org/aqzj2TJh
You can do the same with other way. 您可以使用其他方法执行相同操作。
define("TABLE_PREFIX", "TEST_");
define("TEST_TABLE_QUERY", "select * from ".TABLE_PREFIX."USER");
class Test {
private static $sql_query = TEST_TABLE_QUERY;
public static function show_query1() {
echo "My first test";
echo self::$sql_query;
}
public static function show_query2() {
echo "My second test";
echo self::$sql_query;
}
}
Hope this can help you. 希望这可以帮到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.