gpt4 book ai didi

PHP 面向对象 : Chainable objects?

转载 作者:可可西里 更新时间:2023-11-01 12:23:26 86 4
gpt4 key购买 nike

我试图在 PHP 中找到一个关于可链接 OOP 对象的很好的介绍,但还没有任何好的结果。

这样的事情怎么办?

$this->className->add('1','value');
$this->className->type('string');
$this->classname->doStuff();

甚至:$this->className->add('1','value')->type('string')->doStuff();

非常感谢!

最佳答案

关键是在每个方法中返回对象本身:

class Foo {
function add($arg1, $arg2) {
// …
return $this;
}
function type($arg1) {
// …
return $this;
}
function doStuff() {
// …
return $this;
}
}

返回对象本身的每个方法都可以用作方法链中的中间体。参见 Wikipedia’s article on Method chaining了解更多详情。

关于PHP 面向对象 : Chainable objects?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2929928/

86 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com