PHP Missing Methods & Levenshtein Distance

I’m just going to leave this nifty little PHP snippet here, and recommend against it’s use. It could very well be shorter and more concise, probably a bit more performant too, but it works:

class Example {
public function length(){
return 5;
}
public function __call($name,$arguments){
$methods = get_class_methods($this);
$ldist = 9999;
$currmethod = $name;
foreach($methods as $m){
$distance = levenshtein($name,$m);
if($distance < $ldist){
$currmethod = $m;
$ldist = $distance;
}
}
return call_user_func(array($this,$currmethod),$arguments);
}
}
$example = new Example();
echo $example->elngth();
view raw gistfile1.php hosted with ❤ by GitHub

5 thoughts on “PHP Missing Methods & Levenshtein Distance

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.