开发者

how do closures help in creating a DSL/fluent interface: PHP examples?

Can you give me an example, i开发者_如何学Cn PHP, that shows how closures are helpful in creating a DSL (fluent interface)?

edit: The accepted answer in the following question tells about nested closures. If someone could translate that example to PHP that would be helpful too: Experience with fluent interfaces? I need your opinion!


This is the first example I could think of, it's not great but it gives you an idea:

$db = new Database();
$filteredList = $db->select()
           ->from('my_table')
           ->where('id', 9)
           ->run()
           ->filter(function($record){
            // apply some php code to filter records
        });

There I'd be using fluent interfaces to query my database using some ORM and then applying some filter to the recordset I get. Imagine the run() method returns a RecordSet object which has a filter() method that could be something like this:

public function filter ($callback)
{
    return array_filter($this->recordSet, $callback);
}

Do you get the idea?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜