# Why can constants only be scalars?

- Thread: https://externals.io/message/9726
- Original archive: https://news-web.php.net/php.internals/9726
- Started by Sara Golemon on 2004-05-08T04:16:11+00:00
- Messages: 2
- 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

---

## #9726 — Sara Golemon — 2004-05-08T04:16:11+00:00

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

Long ago I was told constants could only be scalars, so I accepted it.
Certainly ZEND_FUNCTION(define) does the check to ensure it's a scalar, but
why?

For what I can tell a zend_constant is just a container for an ordinary zval
which is refcounted and all that fun stuff.  I don't see any obvious reason
for this restriction, and my tests with those restrictions removed seem safe
enough so far...  Can someone spell it out for me with little words?

-Sara

---

## #9730 — Andi Gutmans — 2004-05-08T07:19:44+00:00

URL: https://externals.io/message/9726#9730 · In reply to: #9726

At 09:16 PM 5/7/2004 -0700, Sara Golemon wrote:
>Long ago I was told constants could only be scalars, so I accepted it.
>Certainly ZEND_FUNCTION(define) does the check to ensure it's a scalar, but
>why?
>
>For what I can tell a zend_constant is just a container for an ordinary zval
>which is refcounted and all that fun stuff.  I don't see any obvious reason
>for this restriction, and my tests with those restrictions removed seem safe
>enough so far...  Can someone spell it out for me with little words?

I don't remember all of the reasons, but the main reason is that it just 
makes sense (IMO). I think constant values should be easily seen when 
reading through source code. Having to read logic to see what values are in 
the constants doesn't make much sense to me. I'd use variables for that.
If we start talking about class constants then it's obviously even more 
important for them to be constants. You can't evaluate values during the 
class definition as you don't have variables defined yet. I wouldn't want 
 `define()` to behave different from const.

Andi
