doc-en icon indicating copy to clipboard operation
doc-en copied to clipboard

Document that constant & defined work with enums

Open HypeMC opened this issue 3 years ago • 6 comments

The constant() & defined() methods can be used with dynamic enum cases names, which doesn't seem to be documented anywhere at the moment.

HypeMC avatar Dec 11 '22 02:12 HypeMC

Thank you for the PR!

I'm not sure if we need to explicitly document this, since it is already documented:

Because cases are represented as constants on the enum itself, they may be used as static values in most constant expressions: […]

@Crell, @iluuu1994, what do you think?

cmb69 avatar Dec 12 '22 14:12 cmb69

TBF this is the first user contributed note of the enum documentation:

https://www.php.net/manual/en/language.enumerations.basics.php

image

So this doesn't seem obvious to everyone.

@cmb69

Because cases are represented as constants on the enum itself, they may be used as static values in most constant expressions: […]

That really refers to enums being valid in other constant expressions, so things like:

const BAR = Foo::Bar;

iluuu1994 avatar Dec 12 '22 14:12 iluuu1994

I'm not against noting this, although my gut sense is that if you need this, you probably are doing something wrong and should be using a Backed enum instead. I'd be more comfortable if the addition on the enum paged included something to that effect.

Crell avatar Dec 12 '22 16:12 Crell

@Crell While I agree with you that most of the time there's probably a better way to do this, it would still be good to have this documented as I have seen it being used for certain cases. I've added a note saying it's discouraged to use this method though.

HypeMC avatar Dec 12 '22 18:12 HypeMC

Thanks all! Okay, so let's document this. I don't think the changelog entries make much sense, though, since enums are generally only available as of PHP 8.1, so there is not much point in hinting at particular changes regarding constant()/defined().

cmb69 avatar Dec 13 '22 12:12 cmb69

@cmb69 Good point, removed https://github.com/php/doc-en/compare/146e4425160cfbe3ca5db0fd971f19e53398be6e..5e5166663a1cbad952cb6c0acae30095e1d016bc

HypeMC avatar Dec 13 '22 13:12 HypeMC