首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >PHP方法链接还是流畅的接口?

PHP方法链接还是流畅的接口?
EN

Stack Overflow用户
提问于 2010-09-16 14:04:37
回答 9查看 93.2K关注 0票数 183

我使用的是PHP5,我听说了面向对象方法中的一个新特性,叫做“方法链接”。它到底是什么?我该如何实现它?

EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2010-09-16 14:10:57

这很简单,真的。您有一系列的mutator methods,它们都返回原始(或其他)对象。这样,您就可以继续在返回的对象上调用方法。

代码语言:javascript
复制
<?php
class fakeString
{
    private $str;
    function __construct()
    {
        $this->str = "";
    }
    
    function addA()
    {
        $this->str .= "a";
        return $this;
    }
    
    function addB()
    {
        $this->str .= "b";
        return $this;
    }
    
    function getStr()
    {
        return $this->str;
    }
}


$a = new fakeString();


echo $a->addA()->addB()->getStr();

这将输出"ab“

Try it online!

票数 357
EN

Stack Overflow用户

发布于 2015-09-27 11:52:51

尝试以下代码:

代码语言:javascript
复制
<?php
class DBManager
{
    private $selectables = array();
    private $table;
    private $whereClause;
    private $limit;

    public function select() {
        $this->selectables = func_get_args();
        return $this;
    }

    public function from($table) {
        $this->table = $table;
        return $this;
    }

    public function where($where) {
        $this->whereClause = $where;
        return $this;
    }

    public function limit($limit) {
        $this->limit = $limit;
        return $this;
    }

    public function result() {
        $query[] = "SELECT";
        // if the selectables array is empty, select all
        if (empty($this->selectables)) {
            $query[] = "*";  
        }
        // else select according to selectables
        else {
            $query[] = join(', ', $this->selectables);
        }

        $query[] = "FROM";
        $query[] = $this->table;

        if (!empty($this->whereClause)) {
            $query[] = "WHERE";
            $query[] = $this->whereClause;
        }

        if (!empty($this->limit)) {
            $query[] = "LIMIT";
            $query[] = $this->limit;
        }

        return join(' ', $query);
    }
}

// Now to use the class and see how METHOD CHAINING works
// let us instantiate the class DBManager
$testOne = new DBManager();
$testOne->select()->from('users');
echo $testOne->result();
// OR
echo $testOne->select()->from('users')->result();
// both displays: 'SELECT * FROM users'

$testTwo = new DBManager();
$testTwo->select()->from('posts')->where('id > 200')->limit(10);
echo $testTwo->result();
// this displays: 'SELECT * FROM posts WHERE id > 200 LIMIT 10'

$testThree = new DBManager();
$testThree->select(
    'firstname',
    'email',
    'country',
    'city'
)->from('users')->where('id = 2399');
echo $testThree->result();
// this will display:
// 'SELECT firstname, email, country, city FROM users WHERE id = 2399'

?>
票数 32
EN

Stack Overflow用户

发布于 2010-09-16 14:11:52

方法链接意味着您可以链接方法调用:

代码语言:javascript
复制
$object->method1()->method2()->method3()

这意味着method1()需要返回一个对象,而method2()则返回method1()的结果。然后,Method2()将返回值传递给method3()。

好文章:http://www.talkphp.com/advanced-php-programming/1163-php5-method-chaining.html

票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3724112

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档