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

php函数总结php

1.普通函数html

2.变量函数闭包

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

 

3.匿名函数(能够实现闭包)函数

  匿名函数(Anonymous functions),也叫闭包函数(Closures),容许临时建立一个没有指定名称的函数,常常用做回调函数(callback)的参数,固然也有其余应用状况oop

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

 

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

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

  闭包的特色:.net

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

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

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

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(), "\n"; echo $bcl2(), "\n";

以上例程的输出相似于:

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(), "\n"; $cl = $cl->bindTo($ob2); echo $cl(), "\n";
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 

相关文章
相关标签/搜索