문제

I started learning php and Recently I've faced a problem with a constant variable in my code. recently I've created the Ninja class in the editor and set a stealth constant to the string "MAXIMUM", then I try to echo it to the page using the scope resolution operator (::).

<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>

Now question is "How can echo the const Variable in php???"

도움이 되었습니까?

해결책

You already accessed it by echo Ninja::stealth;
Try this:
Live Demo : 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;
       }

Output:

Maximum

다른 팁

Or something like this:

<?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();

?>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top