Clicky

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!

asked 12/16/2011 03:20

Nura111's gravatar image

Nura111 ♦♦


5 Answers:
echo class_name::KEYWORDS_THRESHOLD;

http://php.net/manual/en/language.oop5.constants.php
link

answered

amigura's gravatar image

amigura

but withing the clas its the same thing? they dont shoe it in the exmple I cant use this->
link

answered 2011-12-16 at 11:28:18

Nura111's gravatar image

Nura111

1:
2:
3:
4:
5:
6:
7:
8:
9:
class foo {
   const BAR = 'Hello World!';

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

$foo = new foo;
link

answered 2011-12-16 at 11:29:19

Derokorian's gravatar image

Derokorian

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

answered 2011-12-16 at 11:31:03

Nura111's gravatar image

Nura111

Yes:

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
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;
link

answered 2011-12-16 at 11:33:17

Derokorian's gravatar image

Derokorian

Your answer
[hide preview]

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×2
×191

Asked: 12/16/2011 03:20

Seen: 201 times

Last updated: 12/16/2011 04:06