gpt4 book ai didi

php - 使用变量名获取常量值

转载 作者:行者123 更新时间:2023-12-04 16:54:41 29 4
gpt4 key购买 nike

我有一个像下面这样的类(class),

 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;

有没有办法像这样访问?

最佳答案

试试这个代码:

$ref = new ReflectionClass('DTS\eBaySDK\Constants\SiteIds');
$constName = 'US';
echo $ref->getConstant($constName);

我在以下位置找到了这个答案: stackoverflow.com

关于php - 使用变量名获取常量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34220806/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com