1
讓我們假設這個類:自動調用其他幾個方法,一個方法在PHP類
<?php
namespace app;
class SimpleClass {
protected $url = '';
protected $method = 'GET';
public function __construct($url, $method = 'GET')
{
$this->url = $url;
$this->method = $method;
}
public function get()
{
$this->prepare_something();
// other things...
}
public function post()
{
$this->prepare_something();
// other things...
}
public function patch()
{
$this->prepare_something();
// other things...
}
public function put()
{
// other things...
}
public function delete()
{
// other things...
}
protected function prepare_something()
{
// preparing...
}
正如你可以在這個類三種方法看; get, post, patch
我們利用方法preparing_something
,但在方法put, delete
我們沒有。我不得不重複3次$this->prepare_something();
。有一次在這3種方法get, post, patch
。在這3種方法開始時,它是3 lines
的調用。
但想象一下,我們有100個方法。我們使用,在30我們不使用。
有沒有辦法在這70種方法中使用auto-call
這些方法?沒有寫在這70種方法的每一種中$this->prepare_something();
?
這只是疼痛,它不覺得權利必須調用所有的時間同樣的方法$this->prepare_something();
在某些方法...