加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 如何在同一行中对类方法进行多个调用?

发布时间:2020-12-13 16:31:53 所属栏目:PHP教程 来源:网络整理
导读:我在 PHP中有一个问题 在我的php文件中,我创建了以下行: $foo = $wke-template-notify() -type("ERROR") -errno("0x14") -msg("You are not logged.") -page("login.tpl"); 最后,我需要我的$foo变量将返回: $foo-type = "ERROR" $foo-errno= "0x14" $foo-m
我在 PHP中有一个问题
在我的php文件中,我创建了以下行:
$foo = $wke->template->notify()
                     ->type("ERROR")
                     ->errno("0x14")
                     ->msg("You are not logged.")
                     ->page("login.tpl");

最后,我需要我的$foo变量将返回:

$foo->type = "ERROR" 
$foo->errno= "0x14" 
$foo->msg= "You are not logged." 
$foo->page= "login.tpl"

请注意,$wke->模板是我需要调用notify()元素的地方.

通过“ – >”逐个调用类的功能的方式因为该函数返回同一个对象的类.见下面的例子.你会得到这个
class Wke {

    public $type;
    public $errno;
    public $msg;
    public $page;

    public $template = $this;

    public function notify(){
        return $this;
    }

    public function errorno($error){
        $this->errno = $error;
        return $this; // returning same object so you can call the another function in sequence by just ->
    }
    public function type($type){
        $this->type = $type;
        return $this;
    }
    public function msg($msg){
        $this->msg = $msg;
        return $this;
    }
    public function page($page){
        $this->page = $page;
        return $this;
    }
}

整个魔法是返回$这个;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读