【问题标题】:PHP: set value of static property using variablePHP:使用变量设置静态属性的值
【发布时间】:2019-09-12 14:05:37
【问题描述】:

如何使用变量设置静态属性的值。我正在使用条形码类,您可以在其中设置条形码类型,如下所示:

$generator->getBarcode($data[$barcode], $generator::TYPE_CODE_128);

我希望条形码类型像这样传入一个变量:

$barcodeType = 'TYPE_CODE_39';
$generator->getBarcode($data[$barcode], $generator::$barcodeType);

这可能吗?如果是这样,正确的语法是什么?

谢谢

【问题讨论】:

    标签: php oop static


    【解决方案1】:

    您可以为此使用constant() 函数。将格式为class::name:

    的字符串传递给它
    $generator->getBarcode(
        $data[$barcode],
        constant(get_class($generator) . '::' . $barcodeType)
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-10
      • 1970-01-01
      • 1970-01-01
      • 2015-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多