我有一个像bellow一样的课程,
namespace DTS\eBaySDK\Constants;
class SiteIds
{
// United States
const US = 0;
// Canada (English)
const ENCA = 2;
// UK
const GB = 3;
// Australia
const AU = 15;
// Austria
const AT = 16;
// Belgium (French)
const FRBE = 23;
// France
const FR = 71;
// Germany
const DE = 77;
// Motors
const MOTORS = 100;
// Italy
const IT = 101;
// Belgium (Dutch)
const NLBE = 123;
// Netherlands
const NL = 146;
// Spain
const ES = 186;
// Switzerland
const CH = 193;
// Hong Kong
const HK = 201;
// India
const IN = 203;
// Ireland
const IE = 205;
// Malaysia
const MY = 207;
// Canada (French)
const FRCA = 210;
// Philippones
const PH = 211;
// Poland
const PL = 212;
// Singapore
const SG = 216;
}
我可以像下面这样访问它,
echo Constants\SiteIds::US;
但是当我尝试像下面这样访问它时,它无法正常工作,
$country ='US';
echo Constants\SiteIds::$country;
有没有这样的方法可以访问?
答案 0 :(得分:2)
尝试使用此代码:
$ref = new ReflectionClass('DTS\eBaySDK\Constants\SiteIds');
$constName = 'US';
echo $ref->getConstant($constName);
找到了这个答案
答案 1 :(得分:0)
要使用变量获得常量,必须使用constant
函数。该函数需要常量的完全限定名称,在您的情况下包括名称空间:
skip_char<','>(std::cin)
或者来自同一名称空间:
constant("DTS\eBaySDK\Constants\SiteIds::$country")