是否可以创建一个在调用每个函数时自动调用的函数?我希望结果是这样的:
before_functions()
function_1()
before_functions()
function_2()
before_functions()
function_3()
Run Code Online (Sandbox Code Playgroud)
但我想要一个具有以下功能的文件:
function before_functions(){} -> call before each function
Run Code Online (Sandbox Code Playgroud)
和另一个我调用函数的文件:
function_1()
function_2()
function_3()
Run Code Online (Sandbox Code Playgroud)
但我不会before_functions在每个函数中调用..
有不同的方法可以解决您的问题。其中一些已经在评论中提到了。让我们采用最简单的方法来解决您的问题。
神奇的方法 __call()
正如lovelace在评论中所说,对于另一篇堆栈溢出文章中所述的问题已经有一个简单的解决方案。它使用 PHP 自己的魔术方法 __call()。让我们看一个简单的例子。
class Foo
{
protected function before() : void
{
echo "before";
}
protected 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"
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,魔术方法__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"
Run Code Online (Sandbox Code Playgroud)
这种方法比使用__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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我们称为 的基本类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;
}
}
Run Code Online (Sandbox Code Playgroud)
一个简单的人,有名字。
class OnSpeakObserver implements \SplObserver
{
public function update(\SplSubject $subject)
{
foreach ($subject->getGroup() as $person) {
if ($person !== $subject->getSpeaker()) {
echo $person . " says: me!<br>";
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我们的观察者,它实现了本机\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!
Run Code Online (Sandbox Code Playgroud)
你可以转移这个小例子来解决你的问题。观察者可以监听函数的执行,并且每次调用其中一个函数时,观察者都可以在之前执行另一个函数。如本例所示,在群体中的某个人说了某件事之后,观察者除了执行之外什么也不做。你的问题也同样如此。这完全取决于主体的notify方法何时被调用。
如果你有任何问题随时问。
| 归档时间: |
|
| 查看次数: |
3880 次 |
| 最近记录: |