note 111411 added to language.oop5.typehinting

From: Date: Sun, 17 Feb 2013 21:46:17 +0000
Subject: note 111411 added to language.oop5.typehinting
Groups: php.notes 
Request: Send a blank email to php-notes+get-193664@lists.php.net to get a copy of this message
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 ---- Server IP: 69.147.83.199 Probable Submitter: 50.43.86.20 ---- Manual Page -- http://php.net/manual/en/language.oop5.typehinting.php Edit -- https://master.php.net/note/edit/111411 Del: integrated -- https://master.php.net/note/delete/111411/integrated Del: useless -- https://master.php.net/note/delete/111411/useless Del: bad code -- https://master.php.net/note/delete/111411/bad+code Del: spam -- https://master.php.net/note/delete/111411/spam Del: non-english -- https://master.php.net/note/delete/111411/non-english Del: in docs -- https://master.php.net/note/delete/111411/in+docs Del: other reasons-- https://master.php.net/note/delete/111411 Reject -- https://master.php.net/note/reject/111411 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#193664) next »