如何实现静态的链式调用

新手上路,请多包涵

如果光是链式调用的话,在每个方法return $this就行
但是不明白如果是这样

Db::table('**')->where('***','***')->order('***')->find('**');

想这种应该怎么实现

阅读 2.3k
2 个回答
public function table()
{
    return new self();
}

简单示例:

<?php
class Db
{
    public $table;
    
    public $a;
    
    public $b;

    public static function table($table)
    {
        $db = new static;
        $db->table = $table;
        return $db;
    }
    
    public function where($a, $b)
    {
        $this->a = $a;
        $this->b = $b;
        return $this;
    }
}

Db::table('abc')->where('a', 'b');
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题