首頁  >  文章  >  後端開發  >  「求助」关于PHP回调函数的疑问

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

WBOY
WBOY原創
2016-06-23 14:26:14964瀏覽

本帖最后由 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

<?phpif (!defined("__IS_ROOT")) die("Access Denied");global $EventStack;//闭包函数(回调)$fGetSettings = function($oSettingDaemon) {	//处理回调返回的系统设置数据模型	echo $oSettingDaemon;};//请求数据原型$EventStack->addEvent(	"DATA_REQUEST",	serialize(array(		array(			"request" => "settingDaemon"		)	)),	$fGetSettings,	null);?>



/EventStack.php

<?php//事件堆栈处理组件/*数 *///已知的事件戳记/* */if (!defined("__IS_ROOT")) die("Access Denied");class EventStack extends Init{	private $_aWatcherRegistry = array();//已注册的观察者列表	private $_aEventStack = array();//事件堆栈	private $_aCallbackRegistry = array();//已注册的回调函数	function __construct() {	}	function __destruct() {		//将关键信息存储至数据源的Log表		//根据调试开关,决定是否输出调试信息至页面	}	function __toString() {	}	##A##	public function addEvent($sStamp = "ISSUE_TRACK", $sValue, $fHandler = null, $mScope = null) {		/*sStamp: 此条消息的戳记		 *sValue: 序列化的数组		 * fHandler(function): 匿名回调函数		 * mScope(mixed type): 回调函数的上下文环境,			 * null表示传入的handler函数是一个全局函数,			 * 字符串类型表示传入的handler函数是scope类的静态函数,			 * 对象类型表示传入的scope是一个对象,handler函数是对象的一个方法		 */		$this->_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