# Class Constant Finalization

- Thread: https://externals.io/message/97950
- Original archive: https://news-web.php.net/php.internals/97950
- Started by Scott Arciszewski on 2017-01-25T06:48:43+00:00
- Messages: 7
- 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

---

## #97950 — Scott Arciszewski — 2017-01-25T06:48:43+00:00

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

All,

Given that we can now declare a class constant as public, protected, or
private, can we also declare them final in 7.2?

https://3v4l.org/rJG0V

Ideally, this code would error on line 18 rather than 15.

Scott Arciszewski
Chief Development Officer
Paragon Initiative Enterprises <https://paragonie.com

---

## #97951 — Nikita Nefedov — 2017-01-25T07:30:06+00:00

URL: https://externals.io/message/97950#97951 · In reply to: #97950

> 
> 
> All,
> 
> Given that we can now declare a class constant as public, protected, or
> private, can we also declare them final in 7.2?
> 
> https://3v4l.org/rJG0V
> 
> Ideally, this code would error on line 18 rather than 15.
> 
> Scott Arciszewski
> Chief Development Officer
> Paragon Initiative Enterprises <https://paragonie.com>

In my book, ideally, there would be no class constant inheritance as inherited constants don't really make sense when they are known at compile time. People often use this feature in order to get things like this to work:

    class LibLevelClass {
        const FOO = "default";
        public function doSomething() {
            echo "the value of FOO is: " . static::FOO;
        }
    }

    class ExtensionClass {
        const FOO = "custom";
    }

But this approach is error prone due to untyped nature of constants (one could override the constant with an array value). In the end this inheritance is always unnecessary.

---

## #97953 — Stanislav Malyshev — 2017-01-25T08:12:13+00:00

URL: https://externals.io/message/97950#97953 · In reply to: #97950

Hi!

> Given that we can now declare a class constant as public, protected, or
> private, can we also declare them final in 7.2?

That doesn't make much sense to me. Constant belongs to a class. I don't
see much utility in "no class extending from this one can define
constant with the same name". final makes sense when the alternative is
override, and you want to exclude it. But since there's no override
here, no point to exclude anything.


-- 
Stas Malyshev
smalyshev@gmail.com

---

## #97954 — Stanislav Malyshev — 2017-01-25T08:14:11+00:00

URL: https://externals.io/message/97950#97954 · In reply to: #97953

Hi!

> > […]
> 
> That doesn't make much sense to me. Constant belongs to a class. I don't
> see much utility in "no class extending from this one can define
> constant with the same name". final makes sense when the alternative is
> override, and you want to exclude it. But since there's no override
> here, no point to exclude anything.

Adding to this, you kind of can simulate it with static::, as Nikita
described, but if you go as far, you probably know what you're doing and
then you should be allowed to do it. Not that I see much point in doing
it, but if you want to...

-- 
Stas Malyshev
smalyshev@gmail.com

---

## #97955 — netmo.php@gmail.com — 2017-01-25T08:27:30+00:00

URL: https://externals.io/message/97950#97955 · In reply to: #97954

They don't belong to a class only, but to a class and all its descendants.
Ideally, descendants should not inherit constants (or anything static), but
we can't change that now, so final could make sense here. Basically, they
should work exactly like static fields, except that static fields can be
re-assigned, while constants may not.

---

## #97956 — Niklas Keller — 2017-01-25T08:49:00+00:00

URL: https://externals.io/message/97950#97956 · In reply to: #97955

2017-01-25 9:27 GMT+01:00 Wes <netmo.php@gmail.com>:

> They don't belong to a class only, but to a class and all its descendants.
> Ideally, descendants should not inherit constants (or anything static), but
> we can't change that now, so final could make sense here. Basically, they
> should work exactly like static fields, except that static fields can be
> re-assigned, while constants may not.
>

Well, we can deprecate them and remove it with PHP 8.

---

## #97962 — Rowan Collins — 2017-01-25T19:17:03+00:00

URL: https://externals.io/message/97950#97962 · In reply to: #97956

> 2017-01-25 9:27 GMT+01:00 Wes <netmo.php@gmail.com>:
>
> > […]
> Well, we can deprecate them and remove it with PHP 8.
>

You mean, make all constants non-inheritable? Why?

Late static binding is a documented language feature, I don't see why 
it's any weirder for constants to participate in it than static fields 
and methods.

Regards,

-- 
Rowan Collins
[IMSoP]
