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();
在某些方法...