是否有可能创建一个在每个函数被调用时被自动调用的函数?我希望结果是这样的:
before_functions()
function_1()
before_functions()
function_2()
before_functions()
function_3()
但我想要一个功能文件:
function before_functions(){} -> call before each function
还有另一个我调用函数的文件:
function_1()
function_2()
function_3()
但我不会在每个函数中调用before_functions
。
发布于 2019-02-04 02:51:44
有不同的方法来解决你的问题。其中一些已经在评论中提到。让我们用最简单的方法来解决你的问题。
魔术方法__call()
正如lovelace在评论中所说的那样,已经有了一个简单的解决方案,可以在another stack overflow article中解决您的问题。它使用PHP自己的魔术方法__call()。让我们看看一个简单的例子。
class Foo
{
protected function before() : void
{
echo "before";
}
public function after() : void
{
echo "after";
}
public function __call($method, $arguments)
{
if (method_exists($this, $method)) {
$this->before();
return call_user_func_array(array($this, $method), $arguments);
}
}
}
// Testing
$class = new Foo();
$class->after(); // echoes "before->after"
正如您所看到的,神奇的方法__call
为您的目的提供了适当的处理。首先,它检查被调用的方法是否存在,然后在执行被调用的方法之前执行before
方法。当您调用存在的类方法时,将自动调用before
方法。
回调方法
正如注释中所提到的,回调函数可能是一种可能的解决方案,而无需处理类实例。让我们看看回调的例子。
$callback = function()
{
echo "before->";
}
function foo(callable $callback, $bla)
{
$callback();
echo $bla;
}
// example code
foo($callback, 'go and make some coffee');
// output: "before->go and make some coffee"
这种方法甚至比使用__call
方法更简单,因为您只需要一个可调用的函数作为函数的参数。别紧张,嗯?
观测器模式
观察者模式附带了php5中的标准php库,并且更加复杂。我想你的用例太复杂了。为了保持完整,这里有一个简短的例子,观察者模式如何成为解决问题的可用解决方案。
class Group implements SplSubject
{
/**
* persons in this group
* @var \SplObjectStorage
*/
protected $persons;
/**
* observer active in this group
* @var \SplObjectStorage
*/
protected $observers;
/**
* the person, which actually speaks
* @var Person
*/
protected $speaker;
/**
* Initializes our class members and sets an observer for this group
*/
public function __construct()
{
$this->persons = new \SplObjectStorage();
$this->observers = new \SplObjectStorage();
$onSpeakObserver = new OnSpeakObserver($who, $what);
$this->attach($onSpeakObserver);
}
public function add(Person $person) {
$this->persons->attach($person);
}
public function speak(Person $who, $what) {
echo $who . " says: " . $what . "<br>";
$this->speaker = $who;
$this->notify();
}
public function getSpeaker() {
return $this->speaker;
}
public function getGroup() {
return $this->persons;
}
public function attach(\SplObserver $observer) {
$this->observers->attach($observer);
}
public function detach(\SplObserver $observer) {
$this->observers->attach($observer);
}
public function notify() {
foreach ($this->observers as $observer) {
$observer->update($this);
}
}
}
这是我们的基本类group
,应该观察它。一个需要观察的类,总是被称为“主语”。一个主题需要一个或多个观察者,这是被主体的notify
方法所调用的。一个小组由几个人和一个演讲者组成。说话人总是有一个说话者,而另一个人是听众,当说话人说话时,听众就会做出反应。对于听众的反应,我们需要一个观察者。如果演讲者说了什么,这个观察者就会听。观察者直接添加到组的构造函数中。
这个类实现了\SplSubject
接口,它为我们提供了处理观察者的方法attach
、detach
和notify
,我们将它们附加到组中。接下来,我们需要一个人和观察者本身的类。
class Person
{
protected $name = '';
public function __construct(string $name) : void
{
$this->name = $name;
}
public function __toString() : string
{
return $this->name;
}
}
一个有名字的简单的人。
class OnSpeakObserver implements \SplObserver
{
public function update(\SplSubject $subject)
{
foreach ($subject->getGroup() as $person) {
if ($person !== $subject->getSpeaker()) {
echo $person . " says: me!<br>";
}
}
}
}
这是我们的观察者,它实现了本机\SplObserver
接口,这迫使我们使用update方法。当小组中的一个人发言时,这个方法每次都会被调用。
有了这个类,我们就有了一个简单的观察者模式。在这个简单的例子中,观察者在一个群体中的每一个人说什么的时候,都会强迫他们做出反应。
// open a group (the subject, which is observed)
$friends = new Group();
// add some persons to our group
$sarah = new Person('Sarah');
$friends->add($sarah);
$nicole = new Person('Nicole');
$friends->add($nicole);
$marcel = new Person('Marcel');
$friends->add($marcel);
$steffen = new Person('Steffen');
$friends->add($steffen);
// Marcel says ...
$friends->speak($marcel, 'Who likes the observer pattern?');
// result:
// Marcel says: Who likes the observer pattern?
// Sarah says: me!
// Nicole says: me!
// Steffen says: me!
你可以用这个小例子来解决你的问题。观察者可以侦听函数的执行情况,每次调用一个函数时,观察者都可以在此之前执行另一个函数。如本例所示,观察者只会在一个组中的人说了一些话之后执行。你的问题也是如此。这完全取决于何时调用主题的通知方法。
如果你有任何问题,请随便问。
https://stackoverflow.com/questions/54476354
复制相似问题