# How to access the per-class constant from a member function?

- Thread: https://externals.io/message/11592
- Original archive: https://news-web.php.net/php.internals/11592
- Started by Kamesh Jayachandran on 2004-07-28T13:32:01+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #11592 — Kamesh Jayachandran — 2004-07-28T13:32:01+00:00

URL: https://externals.io/message/11592#11592

Hi All,
<?php
class Foo {
   const HELLO = "con\n";
   function memberfunc() {
     echo HELLO; //does not work
     echo Foo::HELLO; //works
   }
}
$obj = new Foo();
$obj->memberfunc();
?>

With a error_reporting=2047 I get 

Notice: Use of undefined constant HELLO - assumed 'HELLO' in
/rekha/php-5.0.0/test.php on line 5
Whey echo HELLO fails?

With regards
Kamesh Jayachandran

---

## #11593 — Mehdi Achour — 2004-07-28T16:10:52+00:00

URL: https://externals.io/message/11592#11593 · In reply to: #11592

Hi Kamesh,

Try replacing Foo::HELLO with self::HELLO. This should work.
(this is documented, the manual will reflect it in the next build)

didou

Kamesh Jayachandran wrote:
> Hi All,
> <?php
> class Foo {
>    const HELLO = "con\n";
>    function memberfunc() {
>      echo HELLO; //does not work
>      echo Foo::HELLO; //works
>    }
> }
> $obj = new Foo();
> $obj->memberfunc();
> ?>
> 
> With a error_reporting=2047 I get 
> 
> Notice: Use of undefined constant HELLO - assumed 'HELLO' in
> /rekha/php-5.0.0/test.php on line 5
> Whey echo HELLO fails?
> 
> With regards
> Kamesh Jayachandran

---

## #11819 — Kamesh Jayachandran — 2004-08-02T13:46:22+00:00

URL: https://externals.io/message/11592#11819 · In reply to: #11593

Hi Mehdi,
Even Foo::HELLO works.
My question is why simple HELLO inside a member function does not work.

Why are we not able to access the static and const members of a class
from inside a class member function like the way with other member
variables.?

Any special reasons.

With regards
Kamesh Jayachandran
On Wed, 28 Jul 2004 18:10:52 +0200, "Mehdi Achour" <didou@php.net> said:
> Hi Kamesh,
> 
> Try replacing Foo::HELLO with self::HELLO. This should work.
> (this is documented, the manual will reflect it in the next build)
> 
> didou
> 
> Kamesh Jayachandran wrote:
> > […]

---

## #11822 — Derick Rethans — 2004-08-02T14:02:23+00:00

URL: https://externals.io/message/11592#11822 · In reply to: #11819

> Hi Mehdi,
> Even Foo::HELLO works.
> My question is why simple HELLO inside a member function does not work.
>
> Why are we not able to access the static and const members of a class
> from inside a class member function like the way with other member
> variables.?

Other member variables don't work either like this, you need to specify
$this-> always.

Derick

---

## #11823 — Kamesh Jayachandran — 2004-08-02T14:14:30+00:00

URL: https://externals.io/message/11592#11823 · In reply to: #11822

Hi Derick,
Thanks for the correction.

With regards
Kamesh Jayachandran
On Mon, 2 Aug 2004 16:02:23 +0200 (CEST), "Derick Rethans"
<derick@php.net> said:
> 
> 
> > […]
> 
> Other member variables don't work either like this, you need to specify
> $this-> always.
> 
> Derick
