方法链的影响 [英] Effects of method chaining
问题描述
我知道在 PHP 中链接的好处,但假设我们有以下情况
I know the benefits of chaining within PHP but lets say we have this following situation
$Mail = new MailClass("mail")
->SetFrom("X")
->SetTo("X")
->SetSubject("X")
->AddRecipient("X")
->AddRecipient("X")
->AddRecipient("X")
->AddRecipient("X")
->AddRecipient("X")
->AddRecipient("X")
->Send();
反复返回和重复使用对象是否存在任何问题,例如速度或未能遵循最佳实践
Are there any issues with returning and reusing the object over and over again, issues such as speed or failure to follow best Practises
如果您是 Fluent-Interface 的新手,也可以很好地阅读:Martin Fowler on Fluent-Interfaces
Also a good read on this if your new to Fluent-Interface's: Martin Fowler on Fluent-Interfaces
我完全理解它没有以这种方式编程,并且可以像这样处理:
I Fully understand that it doesn't have to be programmed this way, and can be handled like so:
$Mail = new MailClass("mail");
$Mail->AddRecipien(
array(/*.....*/)
);
$Mail->SetFrom("X");
$Mail->SetTo("X");
$Mail->SetSubject("X");
$Mail->Send();
但是假设我有一个像这样的对象:
but lets say I have an object like so:
$Order = new Order()
->With(22,'TAL')
->With(38,'HPK')->Skippable()
->With(2,'LGV')
->Priority();
注意 ->With(38,'HPK')->Skippable()
,这是 Pro 进行此类编程的完美示例
Note the ->With(38,'HPK')->Skippable()
, This is perfect example of a Pro for this type of programming
推荐答案
如果您必须验证某事,我认为在 AddRecipient 方法本身中验证它更有意义,但性能应该差不多.而且我不知道使用方法链有什么普遍的缺点.
If you have to validate Something, i think it makes more sense to validate it in the AddRecipient Method itself, but the Performance should be about the same. And I'm not aware of any general disadvantages of using method chaining.
这篇关于方法链的影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!