[英]PHP constant in static variable
我需要在静态变量中使用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();
我不想将常量作为参数传递给静态函数,也不想在每个静态函数中声明$ sql_query。
最好的方法是什么?
编辑:添加演示= http://codepad.org/aqzj2TJh
您可以使用其他方法执行相同操作。
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;
}
}
希望这可以帮到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.