Closure class: Anonymous functions (introduced in PHP 5.3) produce objects of this type.
This class can be bound to a class or object, that is, custom methods are dynamically added to the class or object
Methods used before php7
Closure::bind: Copy a closure and bind the specified $this object and class scope. This method is the static version of Closure::bindTo()
Closure::bindTo: copies the current closure object and binds the specified $this object and class scope. Creates and returns an anonymous function, which has the same function body as the current object and binds the same variables, but can bind different objects or a new class scope.
php7 added
<code><span>//bind.php</span><span><?php</span><span>/** * Created by PhpStorm. * User: bee * Date: 2016/4/24 * Time: 22:35 */</span><span><span>class</span><span>A</span> {</span><span>private</span><span>static</span><span>$sta</span> = <span>1</span>; <span>private</span><span>$com</span> = <span>2</span>; } <span>$cl1</span> = <span>static</span><span><span>function</span><span>()</span> {</span><span>return</span> A::<span>$sta</span>; }; <span>$cl2</span> = <span><span>function</span><span>()</span> {</span><span>return</span><span>$this</span>->com; }; <span>$bcl1</span> = Closure::bind(<span>$cl1</span>, <span>null</span>, <span>'A'</span>); <span>$bcl2</span> = Closure::bind(<span>$cl2</span>, <span>new</span> A(), <span>'A'</span>); <span>echo</span><span>$bcl1</span>(), <span>"\n"</span>; <span>echo</span><span>$bcl2</span>(), <span>"\n"</span>;</span></code>
<code><span><?php</span><span>//bindTo.php</span><span>/** * Created by PhpStorm. * User: bee * Date: 2016/4/24 * Time: 22:35 */</span><span><span>class</span><span>A</span> {</span><span><span>function</span><span>__construct</span><span>(<span>$val</span>)</span> {</span><span>$this</span>->val = <span>$val</span>; } <span><span>function</span><span>getClosure</span><span>()</span> {</span><span>//returns closure bound to this object and scope</span><span>return</span><span><span>function</span><span>()</span> {</span><span>return</span><span>$this</span>->val; }; } } <span>$ob1</span> = <span>new</span> A(<span>1</span>); <span>$ob2</span> = <span>new</span> A(<span>2</span>); <span>$cl</span> = <span>$ob1</span>->getClosure(); <span>echo</span><span>$cl</span>(), <span>"\n"</span>; <span>$add</span> = <span><span>function</span><span>()</span>{</span><span>return</span><span>$this</span>->val+<span>1</span>; }; <span>$cl</span> = <span>$add</span>->bindTo(<span>$ob2</span>); <span>//与call相比,需要增加()方可被调用</span><span>echo</span><span>$cl</span>(), <span>"\n"</span>;</span></code>
<code><span><?php</span><span>//call.php</span><span>/** * Created by PhpStorm. * User: bee * Date: 2016/4/24 * Time: 22:35 */</span><span><span>class</span><span>Value</span> {</span><span>protected</span><span>$value</span>; <span>public</span><span><span>function</span><span>__construct</span><span>(<span>$value</span>)</span> {</span><span>$this</span>->value = <span>$value</span>; } <span>public</span><span><span>function</span><span>getValue</span><span>()</span> {</span><span>return</span><span>$this</span>->value; } } <span>$three</span> = <span>new</span> Value(<span>3</span>); <span>$four</span> = <span>new</span> Value(<span>4</span>); <span>$closure</span> = <span><span>function</span><span>(<span>$delta</span>)</span> {</span><span>return</span><span>$this</span>->getValue() + <span>$delta</span>; }; <span>//可直接调用,不用在后面增加()</span><span>echo</span><span>$closure</span>->call(<span>$three</span>, <span>3</span>); <span>echo</span><span>$closure</span>->call(<span>$four</span>, <span>4</span>); </span></code>
The above has introduced Closure::call of PHP7, including closure and php7. I hope it will be helpful to friends who are interested in PHP tutorials.