如何动态访问类const?

问题描述 投票:1回答:1
class InputCheck
{
   const NOT_INPUT = "%sRequired";

   public function inList ($msg = null)
   {
     print_r(self::$msg);exit;
   }
}

(new InputCheck())->inList('NOT_INPUT');

在此类中,我定义了一个名称为NOT_INPUT的常量。其中一种方法接受包含该常量名称的字符串。如何使用该值动态访问类常量?

php const
1个回答
-1
投票

请参见下面有关如何访问类常量的信息。

class InputCheck
{
const NOT_INPUT = "%sRequired";

   public function inList ($msg = null)
   {
     echo self::NOT_INPUT; //Access the Constant here.
     print_r($msg);exit;
   }
}
© www.soinside.com 2019 - 2024. All rights reserved.