How to chain program from PHP, promise, Android and IOS code


Chained programming or chained writing is to link multiple methods (functions) together into a code by dot (.) or (- >) and other symbols, which can not only enhance the readability of the code, but also operate and change the properties and state of the object every time.

No matter the back-end language or front-end development, many writing methods have changed, and more of them are chain writing, that is, the result of the previous function is directly taken into execution as the starting point or variable of the next method. This paper will simply list the chain writing methods of different languages, which is a summary.

The tap global function was introduced in laravel 5.3

function tap($value, $callback)
    return $value;

Pass in the value value, operate on the value, and finally return the value. The tap function gilds the value, modifies the status and attributes of the value object, and finally returns the result for subsequent operation. For example:

return tap($user)->update([
    'name' => $name,
    'age' => $age,

When we pass in a$userAfter the model is linked to the tap method, we can chain various model functions. Normally, the update method returns a boolean type. Just because we use the tap function, the update method returns a user model object. We can continue to chain the model method and operate the user model object.

In fact, when we write some third-party plug-ins, we usually use chain style routines and writing methods, such as magic methods in PHP__call():

public function __call($method, $args)
    $map = [
        'template' => 'template_id',
        'templateId' => 'template_id',
        'uses' => 'template_id',
        'to' => 'touser',
        'receiver' => 'touser',
        'color' => 'topcolor',
        'topColor' => 'topcolor',
        'url' => 'url',
        'miniprogram' => 'miniprogram',
        'link' => 'url',
        'data' => 'data',
        'with' => 'data',
    if (0 === stripos($method, 'with') && strlen($method) > 4) {
        $method = lcfirst(substr($method, 4));
    if (0 === stripos($method, 'and')) {
        $method = lcfirst(substr($method, 3));
    if (isset($map[$method])) {
        $this->message[$map[$method]] = array_shift($args);
    return $this;

With this magic method, the chain writing method has a great feeling:

yield $this->bnotice

In promise, chaining is also very common

In ES6 promise, the real strength of promises lies in multiple links. When calling promise.then (func) returns a new promise, which will not be executed until the last one is completed.
How to chain program from PHP, promise, Android and IOS code
As long as conditions permit, you can go on forever
How to chain program from PHP, promise, Android and IOS code

Gulp chain writing

Gulp is one of the most popular front-end building tools, and it is also one of the most popular tools on NPM. Its appearance makes front-end development very convenient. It is different from webpack, it belongs to streaming file, simple and clear. among gulp.pipe () the process takes the output of the previous step as the input of the next step
How to chain program from PHP, promise, Android and IOS code

Glide chain

Glide, an image loading library recommended by Google. When I write Android code, I like this chained approach:
How to chain program from PHP, promise, Android and IOS code

Mason chain

In IOS development, when using masonry framework to realize automatic layout, chain writing is also very common

//Set constraints
[demoView mas_makeConstraints:^(MASConstraintMaker *make) {

    //Sets the constraint distance at the top self.view Top 100;
    //Set the constraint on the left
    //Set the constraint on the right
    //Set high



This article simply lists some chained writing methods, whether it’s background language, front-end language, tools, or Android, IOS development. This chained writing method has been highly praised by many developers.

This kind of chain rule is: the object is passed into each method function for logical processing, and then the object is returned, and then the next method is entered again and again. At this time, the properties and objects of the object are changed.

It’s like a person who constantly enters into different environments to temper himself. He enters one environment to temper himself, then leaves, then enters the next environment to gild himself, and finally returns to himself. But at this time, he has been tempered through different environments and become a different self~~~

How to chain program from PHP, promise, Android and IOS code

Recommended Today

Practice of query operation of database table (Experiment 3)

Following the previous two experiments, this experiment is to master the use of select statements for various query operations: single table query, multi table connection and query, nested query, set query, to consolidate the database query operation.Now follow Xiaobian to practice together!Based on the data table (student, course, SC, teacher, TC) created and inserted in […]