How does PHP combine a String constant and a string in a class to define a variable?

Source: Internet
Author: User
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_PATHCall 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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.