How to access const in a php class?

how to access const in a php class?

const  KEYWORDS_THRESHOLD = 0.95;

How to I access it with function within the class?

Thank you!
Nura111Asked:
Who is Participating?
 
DerokorianCommented:
class foo {
   const BAR = 'Hello World!';

   public function __construct() {
      echo self::BAR;
   }
}

$foo = new foo;

Open in new window

0
 
amiguraCommented:
echo class_name::KEYWORDS_THRESHOLD;

http://php.net/manual/en/language.oop5.constants.php
0
 
Nura111Author Commented:
but withing the clas its the same thing? they dont shoe it in the exmple I cant use this->
0
 
Nura111Author Commented:
ok thanks and if a function is public static function foo()

from outside I use name::foo()

but from withing the class same wuestion can I use this->foo as regular function?
0
 
DerokorianCommented:
Yes:

class foo {
   const BAR = "Hello World!";

   public static function boldBar() {
      echo '<strong>'.self::BAR.'</strong>';
   }
   public function __construct() {
      $this->boldBar();
   }
}

foo::boldBar();
$foo = new foo;

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.