Hello All,
Before drafting an RFC I would like to gauge interest in adding:
get_object_constants and get_class_constants
Currently this can only be done through ReflectionClass which is far slower
than retrieving them directly from the constants table. Some simple
timings show that through reflection retrieving these values is 2-3 times
slower than providing a quick access function for retrieval.
This also fits nicely amongst the current stack of:
get_object_vars, get_class_vars and get_class_methods
These functions are commonly asked about on areas such as StackOverflow (
http://stackoverflow.com/questions/956401/can-i-get-consts-defined-on-a-php-class)
amongst other places on the net.
I have already drafted up a PR with the changes and supplemental data:
https://github.com/php/php-src/pull/292
Regards,
Mike
Hello All,
Before drafting an RFC I would like to gauge interest in adding:
get_object_constants and get_class_constantsCurrently this can only be done through ReflectionClass which is far slower
than retrieving them directly from the constants table. Some simple
timings show that through reflection retrieving these values is 2-3 times
slower than providing a quick access function for retrieval.This also fits nicely amongst the current stack of:
get_object_vars, get_class_vars and get_class_methodsThese functions are commonly asked about on areas such as StackOverflow (
http://stackoverflow.com/questions/956401/can-i-get-consts-defined-on-a-php-class
)
amongst other places on the net.I have already drafted up a PR with the changes and supplemental data:
https://github.com/php/php-src/pull/292Regards,
Mike
I don't quite see why we need this. The only argument seems to be
performance and I'm not quite sure why performance would be relevant here.
At least I can't imagine in what kind of code fetching the class constants
is a bottleneck.
Nikita
Before drafting an RFC I would like to gauge interest in adding:
get_object_constants and get_class_constantsI have already drafted up a PR with the changes and supplemental data:
https://github.com/php/php-src/pull/292
You took the time to make a PR, but not an RFC? This should really be
the other way around (if at all).
Currently this can only be done through ReflectionClass which is far slower
than retrieving them directly from the constants table. Some simple
timings show that through reflection retrieving these values is 2-3 times
slower than providing a quick access function for retrieval.I don't quite see why we need this. The only argument seems to be
performance and I'm not quite sure why performance would be relevant here.
At least I can't imagine in what kind of code fetching the class constants
is a bottleneck.
I'm meh on the perf issue. Yeah it's probably there, but it's buried
in the noise and I'm not so sure about use-cases. Not against it for
its own sake though.
This also fits nicely amongst the current stack of:
get_object_vars, get_class_vars and get_class_methods
Yay! Consistency! Boo! Poorly named get_*() methods should have been
called something else from the get-go, but it's too late for that.
Honestly, this is the bit that bugs me most.
These functions are commonly asked about on areas such as StackOverflow (
http://stackoverflow.com/questions/956401/can-i-get-consts-defined-on-a-php-class
)
amongst other places on the net.
I... kinda don't care about this part of the argument. It's called
"Google", and if they can't find it in Reflection, they won't find it
here.
-Sara
Before drafting an RFC I would like to gauge interest in adding:
get_object_constants and get_class_constantsI have already drafted up a PR with the changes and supplemental data:
https://github.com/php/php-src/pull/292You took the time to make a PR, but not an RFC? This should really be
the other way around (if at all).
Yeah, part of it is that I want to learn the internals so if I have to
throw it away; I don't care too much about it. Would be different if I
knew the internals far better than I do then I would likely start on that
path. But I wanted to be able to see what the performance differences
were, how to achieve it as I don't want to be one of those... yeah PHP
should do this but I can't do the work... so I figure I'd start by writing
it and finding help along the way until I can figure it out. :)
Currently this can only be done through ReflectionClass which is far
slower
than retrieving them directly from the constants table. Some simple
timings show that through reflection retrieving these values is 2-3
times
slower than providing a quick access function for retrieval.I don't quite see why we need this. The only argument seems to be
performance and I'm not quite sure why performance would be relevant
here.
At least I can't imagine in what kind of code fetching the class
constants
is a bottleneck.I'm meh on the perf issue. Yeah it's probably there, but it's buried
in the noise and I'm not so sure about use-cases. Not against it for
its own sake though.
Many open source projects frown on the usage of Reflection and
ReflectionClass inside of the code base; this is part of the reason. Sure
I could certainly use ReflectionClass and leverage that instead. The
perception is that Reflection* is slow and as such by most maintainers it
is advised to stay away from it unless you're caching the results. As
stated the performance is not necessarily a huge case here; however, the
more places that this becomes incorporated in a code base the slower things
get overall.
Here is a simple use case of usage where MyClass2 can add in new constants
without having issue:
MyClass {
const SUCCESS = 1;
const FAILURE = -1;
const FAILURE_UNCATEGORIZED = -2;
public function __construct($code) {
$code = (int) $code;
if (!in_array($code, get_object_constants($this))) {
$code = self::FAILURE;
}
}
}
MyClass2 extends MyClass
{
const FAILURE_MYTYPE = -3;
}
This also fits nicely amongst the current stack of:
get_object_vars, get_class_vars and get_class_methodsYay! Consistency! Boo! Poorly named get_*() methods should have been
called something else from the get-go, but it's too late for that.
Honestly, this is the bit that bugs me most.
Yeah; I just attempt to follow whatever is consistent in whichever area it
is.
I guess I am also curious here (not to divert from the thread) but if we do
not want to add in more of these; why don't we deprecate the usage of the
current get_class_vars, get_object_vars, get_class_methods, etc and attempt
to have everyone move over to the Reflection use case?
Obviously this would have to be in a major version but if as a language PHP
is moving things to the reflection use cases then provide it only there?
Seriously not attempting to open up a can of worms here; there has already
been several threads on BC and deprecation in the last month or so overall
on the ML.
These functions are commonly asked about on areas such as StackOverflow
(http://stackoverflow.com/questions/956401/can-i-get-consts-defined-on-a-php-class
)
amongst other places on the net.I... kinda don't care about this part of the argument. It's called
"Google", and if they can't find it in Reflection, they won't find it
here.
That is semi true; I believe most people would locate
get_object_vars/get_class_vars and then are confused when they cannot get
the constants. Still it is easy to locate the reflection so this argument
is easily dropped.
- Mike
Hi!
Before drafting an RFC I would like to gauge interest in adding:
get_object_constants and get_class_constantsCurrently this can only be done through ReflectionClass which is far slower
than retrieving them directly from the constants table. Some simple
timings show that through reflection retrieving these values is 2-3 times
slower than providing a quick access function for retrieval.
What kind of application depends on performance on retrieval of class
constant table? I.e. what is the use case that would be served by this
change?
--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Hello Mike,
thank you! It is a useful feature to me.
My usecase are checks on defined constants:
e.g. on my Bitmasks i want to check if given position-value is valid for
current bitmask: http://3v4l.org/CR2qJ
e.g. in my Exception Handler i check Exception-Codes: if not defined in
Exception class, the underlying api may have changed
...
array get_defined_constants ([ bool |$categorize|= false ] )
As there is no equivalent function for classes, i like it simply for the
sake of completeness too :)
To get rid of performance penalty, i mostly hardcode static arrays with
all the constants (high maintenance costs!).
cryptocompress
Hi Crypto Compress,
big congratz to that name, your mummy Hash Compress and your daddy Image
Compress must be very proud!
SCNR
Am 02/27/2013 03:54 PM, schrieb Crypto Compress:
Hello Mike,
thank you! It is a useful feature to me.
class MyBitmask {
const POS_1 = 1;
// const POS_2 = 2; // reserved/undefined
// const POS_3 = 3; // reserved/undefined
const POS_4 = 4;
I'm developing software with PHP since version 2 and i'm still easily
impressed by such code. So we need get_object_constants and
get_class_constants for such fancy stuff?
Where's the good old
$flag == true; or FLAG == true;?
A bitmask class to shuffle around with class constants using reflection
of itself and a magic method get_class_constants to check a boolean flag?
I just don't get it but maybe i'm to focused to doing things the most
simple and readable/maintainable way.
Maybe, in a few years, c++ will be a beginners language compared to PHP
which gets added new shiny features every day which will only be used by
even more shiny frameworks and the most shiniest, bloated objects one
can imagine.
With every fancy feature added to PHP you kill a kitten!
I'm more with this "Give PHP and it's core developers a rest" so that
the core can be improved/cleaned up instead of adding Java features
people have seen on the university and like them but now they're forced
to use PHP which is like the opposite of Java. Java has it's good
reasons too. Just a tool. Use the tool that fits best, don't use the
hammer as a saw by adding teeth to it.
Cheers,
Frank
--
Frank Schenk
Software Analyst
2e Systems
Tel: +49 - 6196 - 95058 - 30
Fax: +49 - 6196 - 95058 - 94
E-mail: frank.schenk@2e-systems.com
Address: 2e Systems GmbH, Königsteiner Str. 87, D-65812 Bad Soden am Taunus
Company registration: Amtsgericht Königstein (Germany), HRB 7303
Director: Philip Douglas
http://www.2e-systems.com/ - making your business fly!
2013/2/27 Frank Schenk frank.schenk@2e-systems.com
Hi Crypto Compress,
big congratz to that name, your mummy Hash Compress and your daddy Image
Compress must be very proud!SCNR
Am 02/27/2013 03:54 PM, schrieb Crypto Compress:
Hello Mike,
thank you! It is a useful feature to me.
class MyBitmask {
const POS_1 = 1;
// const POS_2 = 2; // reserved/undefined
// const POS_3 = 3; // reserved/undefined
const POS_4 = 4;I'm developing software with PHP since version 2 and i'm still easily
impressed by such code. So we need get_object_constants and
get_class_constants for such fancy stuff?Where's the good old
$flag == true; or FLAG == true;?A bitmask class to shuffle around with class constants using reflection
of itself and a magic method get_class_constants to check a boolean flag?I just don't get it but maybe i'm to focused to doing things the most
simple and readable/maintainable way.Maybe, in a few years, c++ will be a beginners language compared to PHP
which gets added new shiny features every day which will only be used by
even more shiny frameworks and the most shiniest, bloated objects one
can imagine.
I don't see this one in a framework. Just said :)
With every fancy feature added to PHP you kill a kitten!
I'm more with this "Give PHP and it's core developers a rest" so that
the core can be improved/cleaned up instead of adding Java features
people have seen on the university and like them but now they're forced
to use PHP which is like the opposite of Java. Java has it's good
reasons too. Just a tool. Use the tool that fits best, don't use the
hammer as a saw by adding teeth to it.Cheers,
Frank--
Frank Schenk
Software Analyst
2e SystemsTel: +49 - 6196 - 95058 - 30
Fax: +49 - 6196 - 95058 - 94
E-mail: frank.schenk@2e-systems.comAddress: 2e Systems GmbH, Königsteiner Str. 87, D-65812 Bad Soden am Taunus
Company registration: Amtsgericht Königstein (Germany), HRB 7303
Director: Philip Douglashttp://www.2e-systems.com/ - making your business fly!
--
thank you! It is a useful feature to me.
class MyBitmask {
const POS_1 = 1;
// const POS_2 = 2; // reserved/undefined
// const POS_3 = 3; // reserved/undefined
const POS_4 = 4;I'm developing software with PHP since version 2 and i'm still easily
impressed by such code. So we need get_object_constants and
get_class_constants for such fancy stuff?Where's the good old
$flag == true; or FLAG == true;?A bitmask class to shuffle around with class constants using reflection
of itself and a magic method get_class_constants to check a boolean flag?I just don't get it but maybe i'm to focused to doing things the most
simple and readable/maintainable way.
The simple case of things like this is to be able to read extended classes;
say you are enforcing some type of bitmask checking; well extended classes
may not have the same signature. Bitmasks are nothing new; PHP uses
several of them internally and several of them inside of various functions
(generally a bitmask would look more like:
00000 = 0
00001 = 1
00010 = 2
00100 = 4
01000 = 8
10000 = 16
Maybe, in a few years, c++ will be a beginners language compared to PHP
which gets added new shiny features every day which will only be used by
even more shiny frameworks and the most shiniest, bloated objects one
can imagine.With every fancy feature added to PHP you kill a kitten!
This is not necessarily a new feature; it's simply rounding out some of the
already existing functions. We're not talking about new "framework"
features; we are talking about ways to get things done quicker (although
the fractions of microseconds don't made a huge difference). If you think
this is a new fancy feature; you likely think that this is the shiniest:
$class = new Reflection('MyClass');
$constants = $class->getConstants();
I'm more with this "Give PHP and it's core developers a rest" so that
the core can be improved/cleaned up instead of adding Java features
people have seen on the university and like them but now they're forced
to use PHP which is like the opposite of Java. Java has it's good
reasons too. Just a tool. Use the tool that fits best, don't use the
hammer as a saw by adding teeth to it.
Funny enough; this is more PHP like than Java; the Reflection class is more
java oriented and already exists and is not touched in this. We're talking
about global functions. Sure give PHP a rest; but you know... I was
raising this simply for consistency; speed. I am not asking anyone else to
work on this; hell part of the reason I submitted the PR before even
writing an RFC.
I am simply suggesting an alternative to using Reflection whereas:
get_class_constants([object|string]);
get_object_constants([object]);
Do we need both; probably not; the first would likely do.
Am 27.02.2013 16:58, schrieb Mike Willbanks:
I am simply suggesting an alternative to using Reflection whereas:
get_class_constants([object|string]);
get_object_constants([object]);Do we need both; probably not; the first would likely do.
+1 for the first one only
Am 27.02.2013 16:12, Analyst (Frank Schenk) trolled:
bitmask class to shuffle around with class constants
shuffle bits around with clearly named constants:
http://php.net/manual/en/language.operators.bitwise.php
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Security/Acl/Permission/MaskBuilder.php
cryptocompress
I am simply suggesting an alternative to using Reflection whereas:
get_class_constants([object|string]);
get_object_constants([object]);Do we need both; probably not; the first would likely do.
+1 for the first one only
Am 27.02.2013 16:12, Analyst (Frank Schenk) trolled:
bitmask class to shuffle around with class constants
shuffle bits around with clearly named constants:
http://php.net/manual/en/**language.operators.bitwise.phphttp://php.net/manual/en/language.operators.bitwise.php
https://github.com/symfony/symfony/blob/master/src/
Symfony/Component/Security/**Acl/Permission/MaskBuilder.phphttps://github.com/symfony/symfony/blob/master/src/Symfony/Component/Security/Acl/Permission/MaskBuilder.php
This part of the example shows this in practice:
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Security/Acl/Permission/MaskBuilder.php#L192
You can see where it is using the reflection class to grab constants.
These are the types of use cases where this becomes useful; now depending
on how many times this is done in a code base really depends on how useful
the function is over using the reflection class itself.
- Mike
Am 27.02.2013 18:16, schrieb Mike Willbanks:
how many times this is done
a quick search on GitHub for:
- "ReflectionClass" (PHP/Code): 240,922
- "getConstants" (PHP/Code): 22,208
This search is not accurate nor representative but there are some more
usecases in this results.
cryptocompress
https://github.com/search?l=php&q=ReflectionClass&ref=searchresults&type=Code
https://github.com/search?l=php&q=getConstants&ref=searchresults&type=Code
"Get rid of ~10% of all reflection usecases with only one function."
This is really an exorbitant shiny argument on its own. Simple too good
to be true. What am i missing? :)
On Wed, Feb 27, 2013 at 7:12 PM, Crypto Compress <
cryptocompress@googlemail.com> wrote:
"Get rid of ~10% of all reflection usecases with only one function."
This is really an exorbitant shiny argument on its own. Simple too good to
be true. What am i missing? :)
You're missing: Why the heck do we want to get rid of Reflection? It's not
like Reflection was added just for fun. It was added to be actually used.
There is no need to replace (new ReflectionClass($class))->getConstants()
with get_class_constants($class). Do we want to alias all Reflection
methods in this way? Imho this is just absurd.
Nikita
sorry, i pollute this thread with discussion about reflection...
Am 27.02.2013 19:19, schrieb Nikita Popov:
Why the heck do we want to get rid of Reflection?
Do we want to alias all Reflection methods in this way?
definitely not, but:
I can access all properties of an object without reflection e.g. with
foreach.
Why i need reflection (ability to examine at runtime!) to do same for
constants (they will not change at runtime)?
cryptocompress
In fact calls to this function are perfectly cacheable by op-caches and
can be optimized near zero. This would be a great performance gain for
this 10% Reflection usecases.
cryptocompress