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

PHP利用function,use来hook?

WBOY
Release: 2016-06-06 20:48:10
Original
1043 people have browsed it

<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>
Copy after login
Copy after login

这个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>
Copy after login
Copy after login

这个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

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template