There is a constant DATA_PATH, and a string & quot; file_name & quot; how to define a variable in the class (the constant seems to need 5.7), which indicates DATA_PATH and & quot; file_name & quot;. I can define it outside the class as follows: {code ...} but it won't work in the class: {code ...}... there is a constant DATA_PATH and a string "file_name"
How to define a variable in the class (the constant seems to need 5.7), indicating the combination of DATA_PATH and "file_name.
I can define a class as follows:
$FILE_PATH = DATA_PATH."file_name";
But not in the class:
Class Util {public static $ FILE_PATH = DATA_PATH. "file_name"; // syntax error}
Other methods except upgrading PHP
Reply content:
There is a constant DATA_PATH and a string "file_name"
How to define a variable in the class (the constant seems to need 5.7), indicating the combination of DATA_PATH and "file_name.
I can define a class as follows:
$FILE_PATH = DATA_PATH."file_name";
But not in the class:
Class Util {public static $ FILE_PATH = DATA_PATH. "file_name"; // syntax error}
Other methods except upgrading PHP
The original intention of the subject is not to instantiate it.Util::$FILE_PATH
Call this static property?
Yes. Apart from upgrading to PHP v5.6.0, you can directly write the code as follows:
define('DATA_PATH', __DIR__ . DIRECTORY_SEPARATOR);class Util { public static function getFilePath() { return DATA_PATH . 'filename'; }}echo Util::getFilePath();
Http://3v4l.org/td8D4 | http://3v4l.org/ML4Ce
Remove static ..
Static member attributes must be directly assigned values.
Defined ('data _ file') or define ('data _ file', DATA_PATH. "file_name ");
Class Util {
Public static $ FILE_PATH = DATA_FILE;
}
Http://3v4l.org/gXSsn
PS: 3v4l.org is very useful!