如何在变量名称的php中回显常量变量

时间:2014-01-10 14:15:08

标签: php const

我开始学习php,最近我遇到了代码中常量变量的问题。最近我在编辑器中创建了Ninja类,并将隐藏常量设置为字符串“MAXIMUM”,然后我尝试使用范围解析运算符(:)将其回显到页面。

<html>
 <head>
 <title> Scope it Out! </title>
 </head>

<body>

<p>
  <?php
  class Person {

  }
  class Ninja extends Person {
    // Add your code here...
    const stealth = "Maximum";
  }
  // ...and here!
  if(Ninja::stealth){

    echo stealth;
    }

  ?>

  </p>

 </body>

 </html>

现在的问题是“如何在php ???中回显 const变量

2 个答案:

答案 0 :(得分:3)

您已经通过echo Ninja::stealth;访问了它 试试这个:
现场演示:https://eval.in/88040

 class Person {

      }
      class Ninja extends Person {
        // Add your code here...
        const stealth = "Maximum";
      }
      // ...and here!
      if(Ninja::stealth){
        echo Ninja::stealth;
       }

输出:

Maximum

答案 1 :(得分:1)

或类似的东西:

<?php
  class Person {

  }
  class Ninja extends Person {
    // Add your code here...
    const stealth = "Maximum";
    public function getCamo()
    {
        return self::stealth;
    }
  }

  $ningen = new Ninja;
  echo $ningen->getCamo();

?>
相关问题