note 111411 deleted from language.oop5.typehinting by nikic
| From: | nikic@php.net | Date: | Sun, 01 Nov 2020 21:48:31 +0000 |
| Subject: | note 111411 deleted from language.oop5.typehinting by nikic | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-215685@lists.php.net to get a copy of this message | ||
Note Submitter: michaelrfairhurst at gmail dot com
----
The scalar type hinting solutions are all overthinking it. I provided the optimized regex version,
as well as the fastest implementation I've come up with, which just uses strpos. Then I
benchmark both against the TypeHint class.
<?php
function optimized_strpos($ErrLevel, $ErrMessage) {
if ($ErrLevel == E_RECOVERABLE_ERROR
// order this according to what your app uses most
return strpos($ErrMessage, 'must be an instance of string, string')
|| strpos($ErrMessage, 'must be an instance of integer, integer')
|| strpos($ErrMessage, 'must be an instance of float, double')
|| strpos($ErrMessage, 'must be an instance of boolean, boolean')
|| strpos($ErrMessage, 'must be an instance of resource, resource');
}
function optimized_regex($ErrLevel, $ErrMessage) {
if ($ErrLevel == E_RECOVERABLE_ERROR) {
if(preg_match('/^Argument \d+ passed to (?:\w+::)?\w+\(\) must be an instance of
(\w+), (\w+) given/', $ErrMessage, $matches))
return $matches[1] == ($matches[2] == 'double' ? 'float' :
$matches[2]);
}
}
?>
BENCHMARKING Typehint::handleTypehint()
string type hint.....[function it(string $var) {}]............2.1588530540466 seconds
float type hint......[function it(float $var) {}].............2.1563150882721 seconds
integer type hint....[function it(integer $var) {}]...........2.1579530239105 seconds
boolean type hint....[function it(boolean $var) {}]...........2.1590459346771 seconds
BENCHMARKING optimized_regex()
string type hint.....[function it(string $var) {}]............0.88872504234314 seconds
float type hint......[function it(float $var) {}].............0.88528990745544 seconds
integer type hint....[function it(integer $var) {}]...........0.89038777351379 seconds
boolean type hint....[function it(boolean $var) {}]...........0.89061188697815 seconds
BENCHMARKING optimized_strpos()
string type hint.....[function it(string $var) {}]............0.52635812759399 seconds
float type hint......[function it(float $var) {}].............0.74228310585022 seconds
integer type hint....[function it(integer $var) {}]...........0.63721108436584 seconds
boolean type hint....[function it(boolean $var) {}]...........0.8429491519928 seconds