search
HomeBackend DevelopmentPHP TutorialHow to write closures in PHP+Swoole

How to write closures in PHP+Swoole

Oct 15, 2019 am 09:26 AM
swooleClosure

JS programmers always laugh at PHP for not having closures. Today I took the time to write an article to specifically introduce closures in PHP. PHP has added support for anonymous functions since version 5.3. After several versions and iterations to the current PHP5.6 and PHP7, the closure of the PHP language has been very complete. Combined with the event-driven support provided by Swoole, PHP's closure function is very powerful and elegant.

Anonymous function


The anonymous function is the core of the closure. The anonymous function in PHP is actually an object of the Closure class (please note that it is an object) . Different from ordinary object-oriented programming, the code of the anonymous function is written directly at the calling point. There is no need to write an additional class or write the code of the method. The advantage of this is that it is more direct. The following example sets a timer to output hello world every 2 seconds.

Traditional writing method

function timer () {
    echo "hello world";
}
Swoole\Timer::tick(2000, 'timer');

Closure writing method

Swoole\Timer::tick(2000, function () {
    echo "hello world";
});

Traditional writing method of non-closure, you must declare it first A function, and then enter the function name string. The two pieces of code are separated and not intuitive enough. The closure method writes the declaration of the timer and the code to be executed by the timer together, and the logic is very clear and intuitive. Using closure syntax makes it easy to write callback functions. In scenarios such as event-driven programming, sorting, array_walk, etc. that require users to pass in a piece of execution code, closures are written very elegantly.

The more powerful thing about closure is that it can introduce external variables directly at the call site. The method implemented in PHP is the use keyword.

Use syntax


If the timer just now needs to pass in a variable, the traditional writing method can only be achieved through global variables. Unlike JS, PHP's variable introduction is explicit. If you want to reference external variables, you must use use to declare them. JS is implicit, and external variables can be freely manipulated inside anonymous functions without declaration. The advantage of this is that you write less code, but the disadvantage is that it is risky and confusing.

Traditional writing method

$str = "hello world";
function timer () {
    global $str;
    echo $str;
}
Swoole\Timer::tick(2000, 'timer');

Closure writing method

$str = "hello world";
Swoole\Timer::tick(2000, function () use ($str) {
    echo $str;
});

The closure writing method uses use to directly introduce the current $str variable , without using global global variables. In addition, if you are in the event-driven programming mode of swoole, you cannot achieve asynchronous concurrency using global, because there is only one global global variable. If there are multiple client requests at the same time, each request must query the database and output different content. Traditional The programming method is not easy to implement. You need to use a global variable array to save the respective data with the client's ID as the KEY.

Traditional writing method

$requestArray = array();
$dbResultArray = array();
function my_request($request, $response) {
    global $dbResultArray, $requestArray;
    $queryId = $db->query($sql, 'get_result');
    $requestArray[$request->fd] = array($request, $response);
    $dbResultArray[$queryId] = $request->fd;
}
function get_result($queryId, $queryResult) {
    global $dbResultArray, $requestArray;
    list($request, $response) = $requestArray[$dbResultArray[$queryId]];
    $response->end($queryResult);
}
$server->on('request', 'my_request');

Closure writing method

$server->on('request', function ($request, $response) {
    $queryId = $db->query($sql, function ($queryId, $queryResult) use ($request, $response) {
        $response->end($queryResult);
    });
});

The traditional writing method is very complicated and needs to be repeated many times from the global array Save/extract data. The closure is written very concisely and elegantly, and it only takes a few lines of code to achieve the same function. Closure writing is very suitable for writing server programs in asynchronous non-blocking callback mode. Among the currently popular programming languages, only PHP and JS have this capability.

More features of closure


Use anonymous functions in class methods. Versions 5.4 and above do not need to use use to introduce $this, you can directly use anonymous functions Use $this in the function to call the method of the current object. In swoole programming, this feature can be used to reduce the use introduction and passing of the $serv object.

class Server extends Swoole\Server {
    function onReceive($serv, $fd, $reactorId, $data) {
        $db->query($sql, function ($queryId, $queryResult) use ($fd) {
            $this->send($fd, $queryResult);
        }
    }
}

In addition, if you want to modify external variables in the closure function, you can add the & reference symbol to the variable when using. Note that the object type does not need to be added, because in PHP objects are passed by reference instead of value by default.

For more PHP related knowledge, please visit PHP Chinese website!

The above is the detailed content of How to write closures in PHP+Swoole. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:rango.swoole.com. If there is any infringement, please contact admin@php.cn delete
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor