2 回答
TA貢獻(xiàn)2016條經(jīng)驗(yàn) 獲得超9個(gè)贊
如果我對您的理解正確的話,您想要實(shí)現(xiàn)的通常稱為“鏈接”。
要實(shí)現(xiàn)這一點(diǎn),您必須在方法結(jié)束時(shí)返回對象本身,即“this”。
我已經(jīng)相應(yīng)地調(diào)整了你的代碼
<?php
class Das
{
public $a= 'mulut';
public $b = 'anda';
public $c = 'kotor';
public function dor(){
echo $this->a.PHP_EOL;
echo $this->b.PHP_EOL;
echo $this->c.PHP_EOL;
echo PHP_EOL;
return $this;
}
public function dordor(){
echo 'lmao';
echo PHP_EOL;
return $this;
}
}
$s = new Das();
$s->a = 'mulut';
$s->b = 'anda';
$s->c = 'kotor';
$s
->dor()
->dordor();
?>
TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超11個(gè)贊
您正在將方法用作對象。$s->dor()->dordor(); 但是 dor() 不是一個(gè)對象,它是一個(gè)方法。你必須$this->dordor();在你的 dor() 方法代碼的末尾添加它是調(diào)用 dordor() 方法與你調(diào)用 dor() 方法的相同對象。這個(gè):
<?php
class Das
{
public $a= 'mulut';
public $b = 'anda';
public $c = 'kotor';
public function dor(){
echo $this->a.PHP_EOL;
echo $this->b.PHP_EOL;
echo $this->c.PHP_EOL;
echo PHP_EOL;
$this->dordor();
}
public function dordor(){
echo 'lmao';
echo PHP_EOL;
}
}
$s = new Das();
$s->a = 'mulut';
$s->b = 'anda';
$s->c = 'kotor';
$s->dor();
?>
- 2 回答
- 0 關(guān)注
- 128 瀏覽
添加回答
舉報(bào)
