# [PATCH] Allow constant expressions

- Thread: https://externals.io/message/775
- Original archive: https://news-web.php.net/php.internals/775
- Started by Sterling Hughes on 2003-04-08T01:06:51+00:00
- Messages: 8
- 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

---

## #775 — Sterling Hughes — 2003-04-08T01:06:51+00:00

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

Hi,

The attached patch allows constant expressions within class variables
and constants.  I came into this problem when I wanted the following
sequence:

class foo {
    const A = 1<<0;
    const B = 1<<1;
    const C = 1<<2;
}

And the current parser doesn't allow such rules.  I therefore added the
attached patch which allows operators to be used on constants within
these expressions (and also within array declarations).  This is
achieved by folding statements such as '1<<0' into their equivalent
values at compile time.  The following operators are supported: << >> |
& + - * / . % ^ ~ xor

Please review it, and let me know if its ok to commit. :)

-Sterling

-- 
"People can have the Model T in any colour -- so long as it's black." 
    - Henry Ford

---

## #777 — Sterling Hughes — 2003-04-08T02:43:58+00:00

URL: https://externals.io/message/775#777 · In reply to: #775

I've updated my patch (attached), which allows for parentheses groupings
such as :

const foo = (1<<2) | (2 & 5);

-Sterling


> Hi,
> 
> The attached patch allows constant expressions within class variables
> and constants.  I came into this problem when I wanted the following
> sequence:
> 
> class foo {
>     const A = 1<<0;
>     const B = 1<<1;
>     const C = 1<<2;
> }
> 
> And the current parser doesn't allow such rules.  I therefore added the
> attached patch which allows operators to be used on constants within
> these expressions (and also within array declarations).  This is
> achieved by folding statements such as '1<<0' into their equivalent
> values at compile time.  The following operators are supported: << >> |
> & + - * / . % ^ ~ xor
> 
> Please review it, and let me know if its ok to commit. :)
> 
> -Sterling
-- 
"Science is like sex: sometimes something useful comes out, 
but that is not the reason we are doing it." 
    - Richard Feynman

---

## #785 — Zeev Suraski — 2003-04-08T10:37:04+00:00

URL: https://externals.io/message/775#785 · In reply to: #775

Nice patch, but one problem - it allows users to use constants as a part of 
those expressions...  Since the values of those are not known until compile 
time, we have an issue there.  Technically we can disallow such constants 
from being used in those expressions, but I'm not sure how well we can 
explain this (you can use constants, but not in expressions?)  I don't 
know, maybe it's an ok thing to say.

Zeev

At 04:06 08/04/2003, Sterling Hughes wrote:
>Hi,
>
>The attached patch allows constant expressions within class variables
>and constants.  I came into this problem when I wanted the following
>sequence:
>
>class foo {
>     const A = 1<<0;
>     const B = 1<<1;
>     const C = 1<<2;
>}
>
>And the current parser doesn't allow such rules.  I therefore added the
>attached patch which allows operators to be used on constants within
>these expressions (and also within array declarations).  This is
>achieved by folding statements such as '1<<0' into their equivalent
>values at compile time.  The following operators are supported: << >> |
>& + - * / . % ^ ~ xor
>
>Please review it, and let me know if its ok to commit. :)
>
>-Sterling
>
>--
>"People can have the Model T in any colour -- so long as it's black."
>     - Henry Ford

---

## #801 — Sterling Hughes — 2003-04-08T18:05:51+00:00

URL: https://externals.io/message/775#801 · In reply to: #777

Ok, attached is the updated patch, the result of a conversation between
myself and Zeev.

*) Class constants are allowed to be operands to constants.  However,
constants must exist within the current class only, and they must be
defined before usage, so for example:

class simple {
    const second = 1;
    const minute = 60 * second;
    const hour = 60 * minute;
    const day = 24 * hour;
}

Would work.  

However:

class simple {
    const minute = second * 60;
    const second = 1;
}

Wouldn't, and neither would:

class simple {
    const foo = "bar";
}

class simple1 {
    const baz = "bar" . foo;
}

Currently, referencing the current class also doesn't work, ie, 

class simple {
    const bar = "baz";
    const foo = simple::bar . "naz";
}

Although that could be added.

It is also important to note that const is now *fully* compile time,
therefore const cannot, for example, reference a `define()` which is
called above (because `define()` is runtime).

Also, this patch removes the ability to use operators in array
initialization, as a bunch of people seem to have found that behaviour
distasteful.  Patch attached.



> I've updated my patch (attached), which allows for parentheses groupings
> such as :
> 
> const foo = (1<<2) | (2 & 5);
> 
> -Sterling
> 
> 
> > […]
-- 
"Nothing is particularly hard if you divide it into small jobs." 
    - Henry Ford

---

## #802 — Sterling Hughes — 2003-04-08T18:08:25+00:00

URL: https://externals.io/message/775#802 · In reply to: #801

Yargh, a  little debug info got into the last patch.

Attached is the last patch, minus my little debug stuff.



> Ok, attached is the updated patch, the result of a conversation between
> myself and Zeev.
> 
> *) Class constants are allowed to be operands to constants.  However,
> constants must exist within the current class only, and they must be
> defined before usage, so for example:
> 
> class simple {
>     const second = 1;
>     const minute = 60 * second;
>     const hour = 60 * minute;
>     const day = 24 * hour;
> }
> 
> Would work.  
> 
> However:
> 
> class simple {
>     const minute = second * 60;
>     const second = 1;
> }
> 
> Wouldn't, and neither would:
> 
> class simple {
>     const foo = "bar";
> }
> 
> class simple1 {
>     const baz = "bar" . foo;
> }
> 
> Currently, referencing the current class also doesn't work, ie, 
> 
> class simple {
>     const bar = "baz";
>     const foo = simple::bar . "naz";
> }
> 
> Although that could be added.
> 
> It is also important to note that const is now *fully* compile time,
> therefore const cannot, for example, reference a `define()` which is
> called above (because `define()` is runtime).
> 
> Also, this patch removes the ability to use operators in array
> initialization, as a bunch of people seem to have found that behaviour
> distasteful.  Patch attached.
> 
> 
> 
> > […]
-- 
"I can't give you a brain, so I'll give you a diploma"  
    - The Great Oz, The Wizard of Oz

---

## #804 — Sterling Hughes — 2003-04-08T18:15:47+00:00

URL: https://externals.io/message/775#804 · In reply to: #803

> > […]
> 
>  So, only scalars could be initialized consts?
> 

yes, that's how its always worked.

-Sterling
-- 
"Reductionists like to take things apart.  The rest of us are 
 just trying to get it together." 
    - Larry Wall, Programming Perl, 3rd Edition

---

## #803 — Andrey Hristov — 2003-04-08T18:34:18+00:00

URL: https://externals.io/message/775#803 · In reply to: #801

> 
> However:
> 
> class simple {
>     const minute = second * 60;
>     const second = 1;
> }
> 
> Wouldn't, and neither would:
> 
> class simple {
>     const foo = "bar";
> }

 So, only scalars could be initialized consts?



Andrey

---

## #811 — Sterling Hughes — 2003-04-08T21:22:57+00:00

URL: https://externals.io/message/775#811 · In reply to: #802

The last patch introduced a couple of bugs:

1) global constants weren't allowed, this includes true, false, null,
etc.

2) it overrode the default behaviour for var $variable = someconstant; 
since we're folding these, such initialization cannot reasonably happen
(big BC break).

Attached is a new patch (hopefully the last one), which fixes these bugs
and adds the new functionality.

-Sterling



> Yargh, a  little debug info got into the last patch.
> 
> Attached is the last patch, minus my little debug stuff.
> 
> 
> 
> > […]
-- 
"Science is like sex: sometimes something useful comes out, 
but that is not the reason we are doing it." 
    - Richard Feynman
