# Proposal for RFC: get_class_constants()

- Thread: https://externals.io/message/70630
- Original archive: https://news-web.php.net/php.internals/70630
- Started by Stefan Neufeind on 2013-12-14T00:28:50+00:00
- Messages: 13
- 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

---

## #70630 — Stefan Neufeind — 2013-12-14T00:28:50+00:00

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

Hi,

following the howto for RFCs and since this would be my first one, I'd
like to ask for your thoughts if such an RFC might find approval. If
yes, I'll be happy to work on it.

Proposal: Create a function get_class_constants()

It would be similar to `get_class_methods()` and `get_class_vars()`.

Currently the same behavious is only possible by the help of reflection,
which due to the nature of reflection takes a bit more work/cpu-cycles.

  $reflection = new \ReflectionClass($class);
  $constants = $reflection->getConstants();


Kind regards,
 Stefan

---

## #70631 — Kevin Ingwersen — 2013-12-14T00:34:45+00:00

URL: https://externals.io/message/70630#70631 · In reply to: #70630

Hi!

I don’t know much about RFC’s yet - but I would totally vote for that. There are many use cases for that function, and it wouldn’t break anything BC related too.

Kind regards,
Ingwie
Am 14.12.2013 um 01:28 schrieb Stefan Neufeind <neufeind@php.net>:

> Hi,
> 
> following the howto for RFCs and since this would be my first one, I'd
> like to ask for your thoughts if such an RFC might find approval. If
> yes, I'll be happy to work on it.
> 
> Proposal: Create a function get_class_constants()
> 
> It would be similar to `get_class_methods()` and `get_class_vars()`.
> 
> Currently the same behavious is only possible by the help of reflection,
> which due to the nature of reflection takes a bit more work/cpu-cycles.
> 
>  $reflection = new \ReflectionClass($class);
>  $constants = $reflection->getConstants();
> 
> 
> Kind regards,
> Stefan

---

## #70632 — Andrea Faulds — 2013-12-14T00:41:43+00:00

URL: https://externals.io/message/70630#70632 · In reply to: #70630

>
> Currently the same behavious is only possible by the help of reflection,
> which due to the nature of reflection takes a bit more work/cpu-cycles.
>

Unless you're doing this a billion times a second, I can't see why the 
number of cpu cycles for this would matter. And why duplicate 
functionality needlessly, in that case? What's so bad about the current 
Reflection way of doing it, except for performance?

I can't see this proposal really being that useful or going anywhere.

-- 
Andrea Faulds
http://ajf.me/

---

## #70636 — Lester Caine — 2013-12-14T13:33:13+00:00

URL: https://externals.io/message/70630#70636 · In reply to: #70632

Andrea Faulds wrote:
> > […]
>
> Unless you're doing this a billion times a second, I can't see why the number of
> cpu cycles for this would matter. And why duplicate functionality needlessly, in
> that case? What's so bad about the current Reflection way of doing it, except
> for performance?
>
> I can't see this proposal really being that useful or going anywhere.

If it is the only reason you are having to activate reflections? Reflections 
would seem to have a place, but is it really necessary to add yet another layer 
when most of the time it's function is provided better by IDE tools anyway? This 
is a simple example of a current hole in the core PHP tools which would help a 
section of developers who don't need the overheads of additional layers.

-- 
Lester Caine - G8HFL
-----------------------------
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk
Rainbow Digital Media - http://rainbowdigitalmedia.co.uk

---

## #70637 — Marco Pivetta — 2013-12-14T13:51:05+00:00

URL: https://externals.io/message/70630#70637 · In reply to: #70636

If it is the only reason you are having to activate reflections?
Reflections would seem to have a place, but is it really necessary to add
yet another layer when most of the time it's function is provided better by
IDE tools anyway? This is a simple example of a current hole in the core
PHP tools which would help a section of developers who don't need the
overheads of additional layers.

@Lester additional layer is still better than *yet another shortcut*.

Since we already have the feature, introducing another flavor for it seems
a bit of an overkill, especially if it also has to be maintained.

Any userland implementation can handle this by also being fast enuff:

function get_class_constants($className)
{
    static $cached = [];

    if (isset($cached[$className])) {
        return $cached[$className];
    }

    return $cached[$class] = (new \ReflectionClass($class))->getConstants();
}


Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/

---

## #70638 — Robert Stoll — 2013-12-14T13:59:35+00:00

URL: https://externals.io/message/70630#70638 · In reply to: #70637

> -----Original Message-----
> From: Marco Pivetta [mailto:ocramius@gmail.com]
> Sent: Saturday, December 14, 2013 2:51 PM
> To: Lester Caine
> Cc: PHP Internals List
> Subject: Re: [PHP-DEV] Proposal for RFC: get_class_constants()
> 
> 
> 
> If it is the only reason you are having to activate reflections?
> Reflections would seem to have a place, but is it really necessary to add
> yet another layer when most of the time it's function is provided better by
> IDE tools anyway? This is a simple example of a current hole in the core
> PHP tools which would help a section of developers who don't need the
> overheads of additional layers.
> 
> @Lester additional layer is still better than *yet another shortcut*.
> 
> Since we already have the feature, introducing another flavor for it seems
> a bit of an overkill, especially if it also has to be maintained.
> 
> Any userland implementation can handle this by also being fast enuff:
> 
> function get_class_constants($className)
> {
>     static $cached = [];
> 
>     if (isset($cached[$className])) {
>         return $cached[$className];
>     }
> 
>     return $cached[$class] = (new \ReflectionClass($class))->getConstants();
> }
> 
> 
> Marco Pivetta
> 
> http://twitter.com/Ocramius
> 
> http://ocramius.github.com/

I am asking myself why PHP hasn't implement ReflectionClass the way Stefan wants to implement get_class_constants() if his approach is faster. 
I agree that it is somewhat an overkill to have another function for this but maybe Stefan can instead propose to change the implementation for ReflectionClass and all would benefit from it?
On the other hand, it's quite inconsistent to have `get_class_methods()`, `get_class_vars()` but not get_class_constants().

---

## #70640 — Ferenc Kovacs — 2013-12-14T16:32:54+00:00

URL: https://externals.io/message/70630#70640 · In reply to: #70630

2013.12.14. 1:29, "Stefan Neufeind" <neufeind@php.net> ezt írta:
>
> Hi,
>
> following the howto for RFCs and since this would be my first one, I'd
> like to ask for your thoughts if such an RFC might find approval. If
> yes, I'll be happy to work on it.
>
> Proposal: Create a function get_class_constants()
>
> It would be similar to `get_class_methods()` and `get_class_vars()`.
>
> Currently the same behavious is only possible by the help of reflection,
> which due to the nature of reflection takes a bit more work/cpu-cycles.
>
>   $reflection = new \ReflectionClass($class);
>   $constants = $reflection->getConstants();
>
>
> Kind regards,
>  Stefan
>
> --
> 
> 
>

Please go for it!

---

## #70641 — Kalle Sommer Nielsen — 2013-12-15T03:56:20+00:00

URL: https://externals.io/message/70630#70641 · In reply to: #70630

Hi Stefan

2013/12/14 Stefan Neufeind <neufeind@php.net>:
> Hi,
> Proposal: Create a function get_class_constants()

I cooked up a quick little patch, is this what you wanna have included
in the core?

http://pastie.org/855312

Sorry it does not include any phpt's but the code is fairly easy to understand


-- 
regards,

Kalle Sommer Nielsen
kalle@php.net

---

## #70642 — Kalle Sommer Nielsen — 2013-12-15T14:01:19+00:00

URL: https://externals.io/message/70630#70642 · In reply to: #70641

Hi

2013/12/15 Kalle Sommer Nielsen <kalle@php.net>:
> I cooked up a quick little patch, is this what you wanna have included
> in the core?

For some reason the link must have been relocated, heres a new one:
http://pastie.org/8553749



-- 
regards,

Kalle Sommer Nielsen
kalle@php.net

---

## #70644 — Crypto Compress — 2013-12-15T21:12:38+00:00

URL: https://externals.io/message/70630#70644 · In reply to: #70640

Hi,

a short fairy tale:
For a long time thePHPway™ was to build a function for everything 
someone needed/could imagine. This is why PHP is so easy, effective and 
triumphant. But somehow the highlander principle found it's way into PHP 
and nothing will ever be as it was before. So flog the reflection 
argument to death.

http://www.mail-archive.com/internals@lists.php.net/msg64242.html

Cheers!
cryptocompress

---

## #70645 — James Gilliland — 2013-12-15T21:33:33+00:00

URL: https://externals.io/message/70630#70645 · In reply to: #70644

Troll much?

Anyways, along the rest of the thread, a function to support the people
working with objects that don't want to work with objects is a weak place
to argue from. However if we're keeping get_class_* methods, API completion
seems worthwhile.
On Dec 15, 2013 3:13 PM, "Crypto Compress" <cryptocompress@googlemail.com>
wrote:

> Hi,
>
> a short fairy tale:
> For a long time thePHPway™ was to build a function for everything someone
> needed/could imagine. This is why PHP is so easy, effective and triumphant.
> But somehow the highlander principle found it's way into PHP and nothing
> will ever be as it was before. So flog the reflection argument to death.
>
> http://www.mail-archive.com/internals@lists.php.net/msg64242.html
>
> Cheers!
> cryptocompress

---

## #70647 — Rowan Collins — 2013-12-15T23:15:17+00:00

URL: https://externals.io/message/70630#70647 · In reply to: #70644

> Hi,
>
> a short fairy tale:
> For a long time thePHPway™ was to build a function for everything 
> someone needed/could imagine. This is why PHP is so easy, effective 
> and triumphant. But somehow the highlander principle found it's way 
> into PHP and nothing will ever be as it was before. So flog the 
> reflection argument to death.
>
> http://www.mail-archive.com/internals@lists.php.net/msg64242.html
>
> Cheers!
> cryptocompress
>

Buried in the excessive snark of this message is a useful piece of 
information: the proposal has come up before, and an implementation was 
even provided, along with a few use cases which might benefit from the 
small performance improvement of having a direct implementation separate 
from the full ReflectionClass.

It also reminded me of another use case: exceptions. Since the "code" of 
an Exception has to be an integer, writing a readable summary of an 
exception to a text-only log is tricky: if you just echo the code 
itself, you'd have to go back to the source code (or phpdoc) to look up 
its meaning. But when *constructing* an Exception, you will generally 
use a class constant to specify the code. If you grab all the class 
constants of the particular Exception sub-class, you can - hopefully - 
find the one with the appropriate value, and write its name to the log, 
rather than the number.

That look-up may well happen every time you push an exception into the 
logging handler, so keeping it high-performance is probably a good idea. 
I suspect there are other slow points of such a function, but if 
constructing a ReflectionClass carries an inherent cost, it would be 
nice to avoid doing so.

Regards,

-- 
Rowan Collins
[IMSoP]

---

## #70651 — Mike — 2013-12-16T08:50:14+00:00

URL: https://externals.io/message/70630#70651 · In reply to: #70640

> 2013.12.14. 1:29, "Stefan Neufeind" <neufeind@php.net> ezt írta:
> > […]

> Please go for it!

+1

-- 
Regards,
Mike
