Skip to content

Instantly share code, notes, and snippets.

@you-think-you-are-special
Last active August 29, 2015 14:18
Show Gist options
  • Save you-think-you-are-special/96d6b3c85125c49254c4 to your computer and use it in GitHub Desktop.
Save you-think-you-are-special/96d6b3c85125c49254c4 to your computer and use it in GitHub Desktop.
Type hints hack for literal Values in PHP. For a production environment this method is somewhat unusable.
<?php
/**
* Created by Alexander Litvinov
* Email: alexander@codeordie.ru
* May be freely distributed under the MIT license
*/
function typehint($level, $message) {
if($level == E_RECOVERABLE_ERROR && preg_match('/^Argument (\d)+ passed to (?:(\w+)::)?(\w+)\(\) must be an instance of (\w+), (\w+) given/', $message, $match)) {
if($match[4] == $match[5]) {
return true;
}
}
return false;
}
set_error_handler("typehint");
class withHints {
public function a(string $a) {
}
public function b(string $a, integer $b, boolean $c) {
}
}
class withoutHints {
public function a($a) {
}
public function b($a, $b, $c) {
}
}
echo "\n";
$time = microtime(true);
$x = new withoutHints();
for($i = 1000000; $i; --$i) {
$x->a("string");
}
$time = microtime(true) - $time;
echo "Without Hints, one param: ".round($time, 3)."\n";
$time = microtime(true);
$x = new withoutHints();
for($i = 1000000; $i; --$i) {
$x->b("string", 1, true);
}
$time = microtime(true) - $time;
echo "Without Hints, three params: ".round($time, 3)."\n";
$time = microtime(true);
$x = new withHints();
for($i = 1000000; $i; --$i) {
$x->a("string");
}
$time = microtime(true) - $time;
echo "With Hints, one param: ".round($time, 3)."\n";
$time = microtime(true);
$x = new withHints();
for($i = 1000000; $i; --$i) {
$x->b("string", 1, true);
}
$time = microtime(true) - $time;
echo "With Hints, three params: ".round($time, 3)."\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment