Home >Backend Development >PHP Tutorial >PHP利用function,use来hook?

PHP利用function,use来hook?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-06 20:48:101078browse

<code class="lang-php">$hooks = function($meta, $type) use ($inspector, $instance)
            {
                if (isset($meta[$type]))
                {
                    $run = array();

                    foreach ($meta[$type] as $method)
                    {
                        $hookMeta = $inspector->getMethodMeta($method);

                        if (in_array($method, $run) && !empty($hookMeta["@once"]))
                        {
                            continue;
                        }

                        $instance->$method();
                        $run[] = $method;
                    }
                }
            };

            $hooks($methodMeta, "@before");
</code>

这个hooks function里面有一个use是怎么回事?
想不通。。。。望大师告知

回复内容:

<code class="lang-php">$hooks = function($meta, $type) use ($inspector, $instance)
            {
                if (isset($meta[$type]))
                {
                    $run = array();

                    foreach ($meta[$type] as $method)
                    {
                        $hookMeta = $inspector->getMethodMeta($method);

                        if (in_array($method, $run) && !empty($hookMeta["@once"]))
                        {
                            continue;
                        }

                        $instance->$method();
                        $run[] = $method;
                    }
                }
            };

            $hooks($methodMeta, "@before");
</code>

这个hooks function里面有一个use是怎么回事?
想不通。。。。望大师告知

5.3 新增的闭包语法中,use 用来捕捉变量到匿名函数内,见:

http://www.php.net/manual/zh/functions.anonymous.php
http://www.cnblogs.com/melonblog/archive/2013/05/01/3052611.html

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn