I have one specific class in my project that have a lot of static and class methods (this is on purpose, it makes more sense, in this case, to use one big class instead of many smaller ones).
The methods were defined using underscore case like method_name instead of PSR-1 compliant methodName and we are converting a bunch of stuff to PSR-1.
Now, we could use something like these methods (below) to allow the usage of either one (method_name and methodName). The advantage is that we do not risk to introduce new bugs and all the current code using it still works, and we can migrate them slowly.
<?php
// enable PSR-1 for class methods
public static function __callStatic($name, $args) {
$toLower = function($c) { return '_'.strtolower($c[1]); };
$underscoreCased = preg_replace_callback('/([A-Z])/', $toLower, $name);
return forward_static_call_array(['ClassName', $underscoreCased], $args);
}
// enable PSR-1 for instance metods
public function __call($name, $args) {
$toLower = function($c) { return '_'.strtolower($c[1]); };
$underscoreCased = preg_replace_callback('/([A-Z])/', $toLower, $name);
return call_user_func_array([$this, $underscoreCased], $args);
}
Now the question, is this too ugly? Should we avoid doing this despite the benefits listed above?
I wouldn't do it this way for several reasons:
__call()
, __callStatic()
, __get()
, ...) are slow, really, really slow, shown heredo_something
but your code calls Object::doSomething()
. This is an obstacle on my way to check the implementation, as I cannot simply Ctrl+F for doSomething
. The same is true for IDEs and code completion.So, basically my advice: If you really need to migrate this class to PSR-1, do it either all at once or let it be. But don't introduce magic functions only to comply to a naming scheme.