When to use closures in php

清浅
Release: 2023-04-06 15:10:02
Original
4348 people have browsed it

The usage scenarios of closures in PHP are: 1. When calling static classes dynamically; 2. Used in callback functions; 3. Assigned to an ordinary variable; 4. Use use to inherit from the parent domain. ;5. When passing parameters, etc.

When to use closures in php

The usage scenarios of closures in php are: when dynamically calling a static class, use it in the callback function, assign it to an ordinary variable, use use When inheriting from the parent domain and passing parameters

Closure function

Anonymous function, also called closure function (closure), allows temporary Creates a function without a specified name. The value most commonly used as a callback function argument. Of course, there are other applications as well.

Usage scenarios

When dynamically calling a static class

<?php
class test
{
    public static function getinfo()
    {
        var_dump(func_get_args());
    }
}
call_user_func(array(&#39;test&#39;, &#39;getinfo&#39;), &#39;hello world&#39;);
Copy after login

Used in the callback function

<?php
//eg array_walk array_map preg_replace_callback etc
echo preg_replace_callback(&#39;~-([a-z])~&#39;, function ($match) {
    return strtoupper($match[1]);
}, &#39;hello-world&#39;);
// 输出 helloWorld
?>
Copy after login

Assign a value to an ordinary variable

<?php
$greet = function($name)
{
    printf("Hello %s\r\n", $name);
};
$greet(&#39;World&#39;);
$greet(&#39;PHP&#39;);
?>
Copy after login

Use use to inherit from the parent domain

<?php
$message = &#39;hello&#39;;
// 继承 $message
$example = function () use ($message) {
    var_dump($message);
};
echo $example();
// Inherit by-reference
$example = function () use (&$message) {
    var_dump($message);
};
echo $example();
// The changed value in the parent scope
// is reflected inside the function call
$message = &#39;world&#39;;
echo $example();
Copy after login

Pass parameters

<?php
$example = function ($arg) use ($message) {
    var_dump($arg . &#39; &#39; . $message);
};
$example("hello");
Copy after login

Usage in OO

<?php
class factory{
    private $_factory;
    public function set($id,$value){
        $this->_factory[$id] = $value;
    }   
    public function get($id){
        $value = $this->_factory[$id];
        return $value();
    }
}
class User{
    private $_username;
    function __construct($username="") {
        $this->_username = $username;
    }
    function getUserName(){
        return $this->_username;
    }
} 
$factory = new factory();
$factory->set("zhangsan",function(){
    return new User(&#39;张三&#39;);
});
$factory->set("lisi",function(){
   return new User("李四");
});
echo $factory->get("zhangsan")->getUserName();
echo $factory->get("lisi")->getUserName();
Copy after login

Call in function

<?php
 
function call($callback){
            $callback();
    }
call(function() {
    var_dump(&#39;hell world&#39;);
});
Copy after login

The above is the detailed content of When to use closures in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!