PHP鏈?zhǔn)秸{(diào)用如何在中間錯(cuò)誤的時(shí)候拿到錯(cuò)誤信息
這里的錯(cuò)誤信息不是簡(jiǎn)單的字符串,比如鏈?zhǔn)秸{(diào)用過(guò)程中可能某一個(gè)函數(shù)在不滿足某個(gè)條件的時(shí)候,需要返回一個(gè)數(shù)組,直接報(bào)錯(cuò),說(shuō)數(shù)組無(wú)法調(diào)用下一個(gè)函數(shù),但是如何能做到在中間某個(gè)函數(shù)返回的條件下不再往后繼續(xù)調(diào)用呢?
是不是下面的這個(gè)樣子呢?
<?php
class Demo
{
protected $result;
protected $error = false;
function funcA()
{
if (! $this->error) {
//do xxx
}
return $this;
}
function funcB()
{
if (! $this->error) {
//do xxx
//模擬發(fā)生錯(cuò)誤
$this->error = true;
$this->result = ['Ops!', 'Something bad Happened!'];
}
return $this;
}
function funcC()
{
if (! $this->error) {
//do xxx
}
return $this;
}
function GetResult() {
return [$this->result, $this->error];
}
}
$demo = new Demo();
list($result, $hasError) = $demo->funcA()->funcB()->funcC()->GetResult();
var_dump($result, $hasError);
PS: 感覺(jué)寫(xiě)出了
golang
的感覺(jué)
在線把玩 https://glot.io/snippets/ereygerdv3