「求助」关于PHP回调函数的疑问

原创
2016-06-23 14:26:14 688浏览

本帖最后由 kericw 于 2013-11-01 15:37:40 编辑

PHP 回调 回调 PHP

本人较菜,请问各位侠士,如何在PHP中达到EventStack的NotifyWatcher方法可以将取得的数据返还给welcome.php指定的回调函数,我哪里写的不对啊 ???

出现的错误为:Fatal error: Function name must be a string in /home/latel/Workspace/new_zhebo/module/EventStack.php on line ××

/app/welcome.php

addEvent(	"DATA_REQUEST",	serialize(array(		array(			"request" => "settingDaemon"		)	)),	$fGetSettings,	null);?>



/EventStack.php

_aEventStack[] = array(			"stamp" => $sStamp,			"value" => $sValue,			"handler" => $fHandler,			"scope" => $mScope,			"timestamp" => time()		);		$iKey =  sizeof($this->_aEventStack) - 1;		$this->notifyWatcher($iKey);		return $iKey;	}	public function addWatcher($oWatcher, $sWatchStamp) {	}	##C##	public function clearEventStack() {		//清空事件堆栈	}	##E##	public function exportEventStack() {		//输出调试信息	}	##G##	public function getStack($iStackId) {		//根据是否提供堆栈序号,返回堆栈列表或指定堆栈的内容	}	##N##	private function notifyWatcher($iKey) {		//推送事件至相应的观察者		if (array_key_exists($this->_aEventStack[$iKey]["stamp"], $this->_aWatcherRegistry)) {			$mCallback = $this->_aWatcherRegistry[$this->_aEventStack[$iKey]["stamp"]](				$this->_aEventStack[$iKey]["stamp"],				$this->_aEventStack[$iKey]["value"],				$this->_aEventStack[$iKey]["handler"],				$this->_aEventStack[$iKey]["scope"]			);			//如果指定了回调函数,依据观察者返回的数据,做出具体的操作			if (isset($this->_aEventStack[$iKey]["fHandler"]) && !$mCallback) {				$func = $this->_aEventStack[$iKey]["fHandler"];				$func($mCallback);			} else {				$func(null);			}		}	}	##R##	public function removeWatcher($sWatchStamp) {		//移除指定的观察者	}}?>

回复讨论(解决方案)

只看到一次 $fGetSettings

只看到一次 $fGetSettings

版主,可以说的详细点么,我看了您的回答不是很懂。

我现在想这样,

welcome.php页通过EventStack->addEvent的方法将回调函数注册到EventStack中,当EventStack获得相应的数据后将数据带入注册过的回调函数执行。

我语言表达能力不太好。

您的意思是global $fGetSettings么?

Function name must be a string
函数名必须是一个字符串
你没有给出出错位置,我也不好猜测!

既然 $fGetSettings 是闭包,那么你总得在哪里用到他吧?
但你给出的代码片段中并没有

Function name must be a string
函数名必须是一个字符串
你没有给出出错位置,我也不好猜测!

既然 $fGetSettings 是闭包,那么你总得在哪里用到他吧?
但你给出的代码片段中并没有


出错行在EventStack.php的第72行
闭包函数传递给了EventStack.php的37行

闭包当做参数,保存到该对象($EventStack)的私有数组属性 $_aEventStack中了
$EventStack取到数据后会在72行重新从尝试从数组中取出该函数,并将取得的数据作为参数传递给该函数

呵呵,看来我是缺乏提问的艺术。。

求帮忙啊,不然项目就搁在这里了。。。。。。

看来只能靠自己啊

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。