Home >Backend Development >PHP Tutorial >How to use PHP's function variable array to change the code structure (with examples)

How to use PHP's function variable array to change the code structure (with examples)

不言
不言forward
2018-10-16 16:22:561892browse

The content of this article is about how to use PHP’s function variable array to change the code structure (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. help.

The project gets bigger and bigger, the code becomes more and more messy, and maintenance is difficult. There are many reasons. At first, in order to realize the function, we did not pay attention to the structure of the code, it was an outsourcing company. Although the company's project leader has been considering reuse and encapsulation, I feel that the desired effect has not been achieved. Because design patterns are not used in the entire code, this structure is definitely not much better. Although many functions are encapsulated, the layering is particularly messy, and it feels like encapsulation for the sake of encapsulation. In fact, I don’t understand many things, but after writing the code, I will occasionally slightly modify the structure of the code, so as to dig as few holes for myself as possible.

Problems with code structure

The interface has a large number of methods. When calling the interface, you need to use switch to make a judgment. The general structure is as follows:

private function makeXML($xmlName, $xmlNameParam)
{
    $requestData = null;
    switch ($xmlName) {
        // ...
        case 'sendOrder':
            $requestData = $this->sendOrder($xmlNameParam);
            break;
        case 'ecfareQuery':
            $requestData = $this->ecfareQuery($xmlNameParam);
            break;
        case 'getInterAV':
            $requestData = $this->getInterAV($xmlNameParam);
            break;
        // ...
    }
    
    return $requestData;
}

The above case The corresponding methods are called to splice the XML information required by the interface. The parameters of the

method pass the name and parameters of the splicing interface method respectively.

Such a judgment structure code feels difficult to manage. To add a method, you need to add a case call, and it is also messy when you look at it.

Improvement of code structure

Therefore, relevant improvements have been made. The improvements are as follows:

1. First define a method array for saving The method name is defined as follows

protected $arr = [];
public function __construct()
{
    // 初始化接口方法
    $this->arr = [
        'getAV'                    => 'getAV',
        'sendOrder'                => 'sendOrder',
        'ecfareQuery'              => 'ecfareQuery',
        'getInterAV'               => 'getInterAV',
        // ...
    ];
}

If there are new methods under this interface in the future, just add them directly to the array. This saves a lot of switch case judgments.

2. Modify the structure of switch. The code is as follows:

private function makeXML($xmlName, $xmlNameParam)
{
    $requestData = null;
    
    $fun = $this->arr[$xmlName];
    
    $requestData = $this->$fun($xmlNameParam);
    
    return $requestData;
}

Change the structure of switch case to the method of calling by array subscript. In this way, all methods are managed uniformly.

The above ideas come from the MFC framework's processing of Windows messages.

Equivalent to an array of function pointers in C language, or a delegate in C#.

Knowledge supplement:

Process-oriented function variables (look up)

function come() {                   //定义com函数
  echo "来了<p>";
}
function go($name = "jack") {       //定义go函数
  echo $name."走了<p>";
}
function back($string)              //定义back函数
{
  echo "又回来了,$string<p>";
}
$func = "come";                     //声明一个变量,将变量赋值为“come”
$func();                            //使用变量函数来调用函数come()
$func = "go";                       //重新给变量赋值
$func("Tom");                       //使用变量函数来调用函数go()
$func = "back";                     //重新给变量赋值
$func("Lily");                      //使用变量函数来调用函数back();

The object-oriented method in PHP is as follows (implement it by yourself ):

<?php
class test
{
    public $arr = [];
    
    public function __construct()
    {
        $this->arr = array(
            &#39;func1&#39;=>&#39;func1&#39;,
            &#39;func2&#39;=>&#39;func2&#39;,
            &#39;func3&#39;=>&#39;func3&#39;,
            &#39;func4&#39;=>&#39;func4&#39;,
            &#39;func5&#39;=>&#39;func5&#39;,
        );
    }
    public function submit($func, $str)
    {
        $f = $this->arr[$func];
        $this->$f($str);
    }
    static public function func1($str)
    {
        print &#39;func1&#39; . &#39; &#39; . $str . "\n";
    }
    static public function func2($str)
    {
        print &#39;func2&#39; . &#39; &#39; . $str . "\n";
    }

    public function func3($str)
    {
        print &#39;func3&#39; . &#39; &#39; . $str . "\n";
    }
    private function func4($str)
    {
        print &#39;func4&#39; . &#39; &#39; . $str . "\n";
    }
    private function func5($str)
    {
        print &#39;func5&#39; . &#39; &#39; . $str . "\n";
    }
}
$t = new test();        // 实例化类
$f = $t->arr[&#39;func1&#39;];
test::$f(&#39;abc&#39;);        // func1 func2 是静态方法
$f = $t->arr[&#39;func2&#39;];
test::$f(&#39;abc&#39;);
$f = $t->arr[&#39;func3&#39;];  // func3 的调用
$t->$f(&#39;abc&#39;);
// func4 func5 的调用需要使用 submit 方法进行分发
$t->submit(&#39;func4&#39;, &#39;abc&#39;);
$t->submit(&#39;func5&#39;, &#39;bcd&#39;);

The output of the above code is as follows:

func1 abc
func2 abc
func3 abc
func4 abc
func5 bcd

The above is the detailed content of How to use PHP's function variable array to change the code structure (with examples). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete

Related articles

See more