簡體   English   中英

PHP:使用可變方法鏈接方法

[英]PHP: chaining methods with variable methods

這是我的代碼:

return PDF::loadFile($url)
    ->setPaper('a4')
    ->setOption('margin-top', 10)
    ->stream('somefile.pdf');

當我在多個位置調用此方法時,是否存在某種以這種方式調用它的選項?

return PDF::loadFile($url)
    ->callSettings()
    ->stream('somefile.pdf');

設置為->setPaper('a4')->setOption('margin-top', 10)

這很容易。 只需在PDF類中實現一個新的callSettings()方法,即可調用這些方法並返回$this

public function callSettings()
{
    $this->setPaper('a4')
         ->setOption('margin-top', 10);

    return $this;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM