php函数总结(闭包函数,匿名函数)

php函数总结

1.普通函数

2.变量函数

function myfun($a)
{
    echo $a;
}
$b = "myfun";
$b("test");

3.匿名函数(可以实现闭包)

  匿名函数(Anonymous functions),也叫闭包函数(Closures),允许临时创建一个没有指定名称的函数,经常用作回调函数(callback)的参数,当然也有其他应用情况

$func = function() {
};//要带分号
$func() //调用
var_dump($func);
//返回对象类型 object(Closure)#1 (0) { }

4.闭包函数:将匿名函数在普通函数中当做参数出入,也可以被返回,就实现了一个简单的闭包.

  通俗的说,子函数可以使用父函数中的局部变量,这种行为就叫做闭包.

  闭包的特点:

    1.作为一个函数变量的一个引用,当函数返回时,其处于激活状态.

    2.一个闭包就是当一个函数返回时,一个没有释放资源的栈区

--其实上面两点可以合成一点,就是闭包函数返回时,该函数内部变量处于激活状态,函数所在栈区依然保留.

function myfunc()
{
    $a=10;
    $b=11;
    $one = function($str)use(&$a,$b){//use引用外层变量 不加&传副本不影响父函数值
        echo $a=$a+2;
        echo '<br/>';
        echo $b=$b+2;
        echo '<br/>';
        echo $str;
    };
    echo $a;
    echo '---<br/>';
    echo $b;
    echo '---<br/>';
    return $one;
}
$a = myfunc();

$a('你好');

父函数中把匿名函数作为返回值返回,闭包的一种..

 5.内部函数

扩展知识php:USE关键词的用法

1.命名空间

2.闭包函数上下文

3.Trait代码复用时 引用....  (参考http://php.net/manual/zh/language.oop5.traits.php)

Closure 类

  • Closure::__construct — 用于禁止实例化的构造函数
  • Closure::bind — 复制一个闭包,绑定指定的$this对象和类作用域。
  • Closure::bindTo — 复制当前闭包对象,绑定指定的$this对象和类作用域。

Closure::bind

(PHP 5 >= 5.4.0, PHP 7)

Closure::bind — 复制一个闭包,绑定指定的$this对象和类作用域。(深刻理解)

说明

public static Closure Closure::bind ( Closure $closure , object $newthis [, mixed $newscope = 'static' ] )

这个方法是 Closure::bindTo() 的静态版本。查看它的文档获取更多信息。

参数

closure

需要绑定的匿名函数。

newthis

需要绑定到匿名函数的对象,或者 NULL 创建未绑定的闭包。

newscope

想要绑定给闭包的类作用域,或者 'static' 表示不改变。如果传入一个对象,则使用这个对象的类型名。 类作用域用来决定在闭包中 $this 对象的 私有、保护方法 的可见性。 The class scope to which associate the closure is to be associated, or 'static' to keep the current one. If an object is given, the type of the object will be used instead. This determines the visibility of protected and private methods of the bound object.

返回值

返回一个新的 Closure 对象 或者在失败时返回 FALSE

范例

class A {
    private static $sfoo = 1;
    private $ifoo = 2;
}
$cl1 = static function() {
    return A::$sfoo;
};
$cl2 = function() {
    return $this->ifoo;
};

$bcl1 = Closure::bind($cl1, null, 'A');
$bcl2 = Closure::bind($cl2, new A(), 'A');
echo $bcl1(), "
";
echo $bcl2(), "
";

以上例程的输出类似于:

1
2

参见

Closure::bindTo

(PHP 5 >= 5.4.0, PHP 7)

Closure::bindTo — 复制当前闭包对象,绑定指定的$this对象和类作用域

说明

public Closure Closure::bindTo ( object $newthis [, mixed $newscope = 'static' ] )

创建并返回一个 匿名函数, 它与当前对象的函数体相同、绑定了同样变量,但可以绑定不同的对象,也可以绑定新的类作用域。

“绑定的对象”决定了函数体中的 $this 的取值,“类作用域”代表一个类型、决定在这个匿名函数中能够调用哪些 私有 和 保护 的方法。 也就是说,此时 $this 可以调用的方法,与 newscope 类的成员函数是相同的。

静态闭包不能有绑定的对象( newthis 参数的值应该设为 NULL)不过仍然可以用 bubdTo 方法来改变它们的类作用域。

This function will ensure that for a non-static closure, having a bound instance will imply being scoped and vice-versa. To this end, non-static closures that are given a scope but a NULL instance are made static and non-static non-scoped closures that are given a non-null instance are scoped to an unspecified class.

Note:

如果你只是想要复制一个匿名函数,可以用 cloning 代替。

参数

newthis

绑定给匿名函数的一个对象,或者 NULL 来取消绑定。

newscope

关联到匿名函数的类作用域,或者 'static' 保持当前状态。如果是一个对象,则使用这个对象的类型为心得类作用域。 这会决定绑定的对象的 保护、私有成员 方法的可见性。

返回值

返回新创建的 Closure 对象 或者在失败时返回 FALSE

范例

Example #1 Closure::bindTo() 实例

class A {
    function __construct($val) {
        $this->val = $val;
    }
    function getClosure() {
        //returns closure bound to this object and scope
        return function() { return $this->val; };
    }
}

$ob1 = new A(1);
$ob2 = new A(2);

$cl = $ob1->getClosure();
echo $cl(), "
";
$cl = $cl->bindTo($ob2);
echo $cl(), "
";
class A{
    private  $aa = '';
}
$fun = function ()
{
    $this->aa = "31";
};
$cl = $fun->bindTo(new A(),'A');
$cl();

http://php.net/manual/zh/class.closure.php 

http://php.net/manual/zh/closure.bindto.php

上一篇文章:http://www.cnblogs.com/fps2tao/p/8727248.html 

原文地址:https://www.cnblogs.com/fps2tao/p/8727482.html