Last active
December 14, 2015 13:38
-
-
Save phuedx/5094530 to your computer and use it in GitHub Desktop.
Hey, I've just called you,
And this is crazy,
But I misspelled you,
So proxy, maybe?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class CallMeMaybe | |
{ | |
public function __call($name, $arguments) | |
{ | |
$class = new ReflectionClass($this); | |
$weightedMethods = array(); | |
foreach ($class->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { | |
$methodName = $method->getName(); | |
$weight = levenshtein($name, $methodName); | |
$weightedMethods[$methodName] = $weight; | |
} | |
asort($weightedMethods); | |
$methods = array_keys($weightedMethods); | |
$method = $methods[0]; | |
return call_user_func_array(array($this, $method), $arguments); | |
} | |
public function helloWorld() | |
{ | |
return __FUNCTION__; | |
} | |
} | |
$callMeMaybe = new CallMeMaybe(); | |
var_dump($callMeMaybe->helloWrld()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Much cleaner! It's still pretty dirty though...