-
-
Notifications
You must be signed in to change notification settings - Fork 132
Prefix all function calls with a backspace #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I've left a few comments inline
@@ -41,7 +41,7 @@ | |||
public function __construct($value) | |||
{ | |||
if (!$this->isValid($value)) { | |||
throw new \UnexpectedValueException("Value '$value' is not part of the enum " . get_called_class()); | |||
throw new \UnexpectedValueException("Value '$value' is not part of the enum " . \get_called_class()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's necessary to optimize for error cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. I've simply put a backslash before all function calls.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK let's leave it that way. You're right it's no big deal.
$class = get_called_class(); | ||
if (!array_key_exists($class, static::$cache)) { | ||
$class = \get_called_class(); | ||
if (!isset(static::$cache[$class])) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure this change doesn't introduce BC breaks?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. static::$cache
can only be instances of array, for which isset
returns the exact values as array_key_exists
. The only difference between the two is their behaviour around null values.
@@ -181,7 +181,7 @@ public static function __callStatic($name, $arguments) | |||
return new static($array[$name]); | |||
} | |||
|
|||
throw new \BadMethodCallException("No static method or enum constant '$name' in class " . get_called_class()); | |||
throw new \BadMethodCallException("No static method or enum constant '$name' in class " . \get_called_class()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, no need to optimize error cases.
Thanks! |
You're welcome! |
I'm curious. Did somebody measure the actual performance impact of these changes? |
Symfony people did a few months/years ago IIRC. The summary I kept in mind was: it doesn't really matter, except for low level libs where it _could_make a small improvement in some applications. |
This prefixed all function calls with a back space.
This saves PHP some opcodes because it doesn't have to do the namespace lookup.
Secondly, PHP 7.2 introduced a dedicated opcode for
in_array
, but only if it is prefixed with a backspace (and the third argument istrue
). An opcode forget_called_class
was also added to PHP 7.2.See https://phpinternals.net/articles/optimising_internal_functions_via_new_opcode_instructions for background on the new opcodes.