# Undefined constants producing E_NOTICE

- Thread: https://externals.io/message/42357
- Original archive: https://news-web.php.net/php.internals/42357
- Started by Kuba Wieczorek on 2008-12-19T23:43:45+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

---

## #42357 — Kuba Wieczorek — 2008-12-19T23:43:45+00:00

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

Hello everyone,

Currently when an undefined constant is called, `E_NOTICE` is produced and 
the name of the constant is assumed to be a string. In my opinion this 
behaviour is strange and problematic for most developers. Maybe it would 
be good if invoking undefined constants will start to produce at least 
E_WARNING?

For the moment some unexpected behaviour caused by use of undefined 
constant may be hard to fix with low error reporting level. Moreover, 
treating an undefined constant as a string does not make sense. I know 
that PHP is intended to be a flexible language, but for me it's a bit thick.

Regards,
faw

---

## #42358 — Steph Fox — 2008-12-20T11:04:07+00:00

URL: https://externals.io/message/42357#42358 · In reply to: #42357

Hi Kuba,

> For the moment some unexpected behaviour caused by use of undefined 
> constant may be hard to fix with low error reporting level.

So don't use a low error reporting level.

Moreover,
> treating an undefined constant as a string does not make sense. I know 
> that PHP is intended to be a flexible language, but for me it's a bit 
> thick.

Just how is PHP supposed to know that some random string is intended to be 
anything else?

- Steph

---

## #42359 — Stan Vassilev | FM — 2008-12-20T11:25:49+00:00

URL: https://externals.io/message/42357#42359 · In reply to: #42358

> Hi Kuba,
>
> > […]
>
> So don't use a low error reporting level.
>
> Moreover,
> > […]
>
> Just how is PHP supposed to know that some random string is intended to be 
> anything else?

By the quotes :).

Undefined variable $foo doesn't fall back to string 'foo' it falls back to 
null. It's a sane strategy for constants as well.

I have no information how people "rely" on this, I know the reason for this 
fallback is bad syntax like:  echo $hi[there];

I'd fix it in 6.0 though.

Regards, Stan Vassiev

---

## #42360 — crrodriguez@suse.de — 2008-12-20T13:34:50+00:00

URL: https://externals.io/message/42357#42360 · In reply to: #42357

Kuba Wieczorek escribió:
> Hello everyone,
> 
> Currently when an undefined constant is called, `E_NOTICE` is produced and
> the name of the constant is assumed to be a string. In my opinion this
> behaviour is strange and problematic for most developers. Maybe it would
> be good if invoking undefined constants will start to produce at least
> E_WARNING?

Use class constants

class foo {
}

echo foo::BAR;

Fatal error: Undefined class constant 'BAR' ...

that's what you are really looking for... ;)


-- 
"We have art in order not to die of the truth" - Friedrich Nietzsche

Cristian Rodríguez R.
Platform/OpenSUSE - Core Services
SUSE LINUX Products GmbH
Research & Development
http://www.opensuse.org/

---

## #42361 — Greg Beaver — 2008-12-20T16:40:31+00:00

URL: https://externals.io/message/42357#42361 · In reply to: #42360

Cristian Rodríguez wrote:
> Kuba Wieczorek escribió:
> > […]
> 
> Use class constants
> 
> class foo {
> }
> 
> echo foo::BAR;
> 
> Fatal error: Undefined class constant 'BAR' ...
> 
> that's what you are really looking for... ;)

namespaced constants also cause fatal error when used with qualified name.

Greg
