I am working with an Authorize.net code sample that had a file called SampleCodeConstants.php which includes variables like so:
class SampleCodeConstants
{
const HOME_URL = 'http://example.com';
}
This works fine if I can hard code whatever, but I am working on a Wordpress plugin so whatever needs to be set dynamically. For instance, if whatever needs to be the domain name using the plugin I will need to include Wordpress and run site_url(). To do this I added some code for setting whatever according to criteria unique to the plugin and then replaced whatever with something like $whatever.
The new code then looks something like this:
class SampleCodeConstants
{
const HOME_URL = site_url();
}
Now instead of setting the const variables to $whatever I get:
Fatal error: Constant expression contains invalid operations
You can't and you shouldn't. Use a static variable instead.
class SampleCodeConstants {
public static $HOME_URL = site_url();
}
You can then reference this as
SampleCodeConstants::$HOME_URL
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.