可以注销关闭功能吗? [英] Can you unregister a shutdown function?
问题描述
在PHP中是否可以注销使用register_shutdown_function()
设置的功能(或所有功能)?
Is it possible in PHP to unregister a function (or all functions) set up with register_shutdown_function()
?
推荐答案
我不相信你可以.取消注册回调函数的唯一方法是将其包装在仅根据某些条件实际调用它的某种类中.像这个方便的函数包装器一样:
I do not believe you can. The only way to unregister a callback function is to wrap it in some sort of class that only actually calls it based on certain conditions. Like this handy function wrapper:
class UnregisterableCallback{
// Store the Callback for Later
private $callback;
// Check if the argument is callable, if so store it
public function __construct($callback)
{
if(is_callable($callback))
{
$this->callback = $callback;
}
else
{
throw new InvalidArgumentException("Not a Callback");
}
}
// Check if the argument has been unregistered, if not call it
public function call()
{
if($this->callback == false)
return false;
$callback = $this->callback;
$callback(); // weird PHP bug
}
// Unregister the callback
public function unregister()
{
$this->callback = false;
}
}
基本用法:
$callback = new UnregisterableCallback(array($object, "myFunc"));
register_shutdown_function(array($callback, "call"));
要取消注册
$callback->unregister();
现在,当被调用时,它将返回false而不调用您的回调函数.可能有几种方法可以简化此过程,但是可以.
Now, when called, it will return false and not call your callback function. There might be a few ways to streamline this process, but it works.
一种简化的方法是将回调的实际注册放入回调函数的方法中,因此外界不必知道您必须传递给register_shutdown_function的方法是调用"
One such way of streamlining it would be to put the actual registration of the callback into a method of the callback function, so the outside world does not have to have knowledge that the method you have to pass to register_shutdown_function is "call".
这篇关于可以注销关闭功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!