简体   繁体   中英

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?

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.

What is the best way to to it ?

EDIT : add demo = 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM