PHP (index.php):
<?php
class codeboxy {
protected $str;
protected $num = 0;
public function a() {
$this->str .= 'a';
$this->num += 1;
return $this;
}
public function b() {
$this->str .= 'b';
$this->num += 2;
return $this;
}
public function display() {
return $this->str . $this->num;
}
}
$test = new codeboxy();
$test->a()->b()->a()->b(); // "a b a b" . (1 + 2 + 1 + 2)
echo $test->display(); // abab6
?>
結果:說明:
使用PHP連貫的執行Function並Return回物件。
沒有留言:
張貼留言