php - 我可以集成具有相同内容,不同名称的函数吗?

php  - 我可以集成具有相同内容,不同名称的函数吗?

问题描述:

I have a couple of functions inside a class that essentially do the same thing:

public function fn_a(){
        return __FUNCTION__;
}
public function fn_b(){
        return __FUNCTION__;
}
public function fn_c(){
        return __FUNCTION__;
}

I need those functions to remain in their current names so I intentionally did not do:

public function fn_($letter){
        return __FUNCTION__.$letter;
}

I was hoping for some sort of way to minify the verboseness of code here, since they all do the same. The ultimate situation would be something like this:

public functions fn_a, fn_b, fn_c() {
      return __FUNCTION__;
}

Another solution, if applicable, might be doing something like Class's "extends": fn_b, fn_c extend fn_a?

What do you think guys?

我在类中有一些基本上做同样事情的函数: p>

  public function fn_a(){
 return __FUNCTION __; 
} 
公共函数fn_b(){
 return __FUNCTION __; 
} 
公共函数fn_c(){
 return __FUNCTION __; 
}  
  code>  pre> 
 
 

我需要这些功能保留在当前名称中,所以我故意做不 strong>做: p> public function fn _($ letter){ return __FUNCTION __。$ letter; } code> pre>

我希望有某种方式 在这里缩小代码的冗长程度,因为它们都是一样的。 最终的情况是这样的: p>

 公共函数fn_a,fn_b,fn_c(){
 return __FUNCTION __; 
} 
  code>  pre  > 
 
 

另一种解决方案(如果适用)可能会执行类似“扩展”的操作: fn_b,fn_c extend fn_a code>? p>

你觉得怎么样? p> div>

Any syntax like the one you suggested is not possible : if you want several distinct functions, you have to declare all those functions.

Still, a possibility could be that your fn_a, fn_b and fn_c functions just be simple wrappers arround a more complex one :

public function fn_a(){
    return fn_all('a');
}
public function fn_b(){
    return fn_all('b');
}
public function fn_c(){
    return fn_all('c');
}

public function fn_all($name) {
    // a lot of complex stuff here
}

With that, depending on the length on the fn_all function, of course, you would reduce the amount of code-duplication.



Another idea (not sure how this could be done with methods, so I'll demonstrate with functions) would be to use Closures -- which means PHP >= 5.3

The basic idea being that you'd have a first function, that would return another one -- which would bind the parameter passed to the first one :

First, the function that creates the others :

function creator($name) {
    return function () use ($name) {
        return $name;
    };
}

And, then, let's get three functions, using that creator one :

$fn_a = creator('a');
$fn_b = creator('b');
$fn_c = creator('c');

And now, calling those three functions :

echo $fn_a() . '<br />';
echo $fn_b() . '<br />';
echo $fn_c() . '<br />';

We get the following output :

a
b
c


I've never good at explaining how anonymous functions and closures work -- but searching for "closure" on google should help you understand ; note that you can read tutorial about closures in Javascript : the idea is exactly the same.

(And, as closures are new in PHP -- arrived with PHP 5.3 -- you will not find as many tutorials as for Javascript)

public function fn_catcher($letter) {
        return __FUNCTION__.$letter;
}

public function __call($name) {
        if (substr($name, 0, 3) == 'fn_')
        {
              return $this->fn_catcher($name);
        }
}

Like that?