Skip to content

Improvement: get_class_constants and get_object_constants functions - #292

Closed
mwillbanks wants to merge 1 commit into
php:PHP-5.5from
mwillbanks:topic-classobjectconstants
Closed

mwillbanks wants to merge 1 commit into
php:PHP-5.5from
mwillbanks:topic-classobjectconstants

Conversation

@mwillbanks

Copy link
Copy Markdown

Overview

Presently the only way to get class constants in a list is to go through the Reflection API: http://php.net/manual/en/reflectionclass.getconstants.php. While the reflection class object is great; it is also far slower than doing this directly in a zend function. Additionally it does not fit in with the current methods:

  • get_class_methods
  • get_class_vars
  • get_object_vars

In my opinion this should be added for consistency as well as to provide a quick[er] and fast[er] way of accessing the constants of a class than having to utilize reflection for a simple call.

There was a previous ticket on this that recommended the ReflectionClass over providing methods: https://bugs.php.net/bug.php?id=43315 However, I still do believe that these functions are needed and are far more efficient.

Speed Differences:

Test Class

function get_reflection_constants($class)
{
    $rc = new ReflectionClass($class);
    return $rc->getConstants();
}

class MyClass {
    const CONST1 = "var";
    const CONST2 = "var";
}

echo "Reflection Class" . PHP_EOL;
$benchStart = microtime(true);
for ($c=0;$c<100000;$c++) {
    get_reflection_constants('MyClass');
}
$benchEnd = microtime(true);
$diff = $benchEnd - $benchStart;
var_dump($diff);

echo "Object Constants" . PHP_EOL;
$benchStart = microtime(true);
for($c=0;$c<100000;$c++) {
    get_object_constants(new MyClass());
}
$benchEnd = microtime(true);
$diff = $benchEnd - $benchStart;
var_dump($diff);

echo "Class Constants" . PHP_EOL;
$benchStart = microtime(true);
for($c=0;$c<100000;$c++) {
    get_class_constants('MyClass');
}
$benchEnd = microtime(true);
$diff = $benchEnd - $benchStart;
var_dump($diff);

Output:

Reflection Class
float(0.079263210296631)
Object Constants
float(0.031561136245728)
Class Constants
float(0.028576850891113)

@fruitl00p

Copy link
Copy Markdown

I've always adhered to the standard: try to avoid reflection in production code at all costs... (or cache it like crazy)
as a follower of the internals (but without Cskills / karma) i'm +1

But this might also diffuse the whole reflection use case ... no? One might even go as far as using these new methods as part of a internal rewrite of reflection and thus making my previous comment about reflection use in production moot :)

@awartoft

Copy link
Copy Markdown

+1

@mwillbanks

Copy link
Copy Markdown
Author

Closing as there does not seem to be enough interest in creating these methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants