当前位置:Gxlcms > PHP教程 > 有关PHP回调函数的乌云。

有关PHP回调函数的乌云。

时间:2021-07-01 10:21:17 帮助过:7人阅读

本人较菜,请问各位侠士,如何在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

  1. <!--?php if (!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
  1. <!--?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表 //根据调试开关,决定是否</pre-->输出调试信息至页面 } 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) { //移除指定的观察者 }} ?>


回复讨论(解决方案)

还是那样,连错误信息都不给全!

  1. $fGetSettings = function($oSettingDaemon) { //处理回调返回的系统设置数据模型 echo $oSettingDaemon;};$EventStack = new EventStack;//请求数据原型$EventStack->addEvent( "DATA_REQUEST", serialize(array( array( "request" => "settingDaemon" ) )), $fGetSettings, null);class EventStack { 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) {print_r(func_get_args());//传入正确 /*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() );print_r($this->_aEventStack);//赋值正确 $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) { //移除指定的观察者 }}
  1. Array( [0] => DATA_REQUEST [1] => a:1:{i:0;a:1:{s:7:"request";s:13:"settingDaemon";}} [2] => Closure Object ( [parameter] => Array ( [$oSettingDaemon] => <required> ) ) [3] => )Array( [0] => Array ( [stamp] => DATA_REQUEST [value] => a:1:{i:0;a:1:{s:7:"request";s:13:"settingDaemon";}} [handler] => Closure Object ( [parameter] => Array ( [$oSettingDaemon] => <required> ) ) [scope] => [timestamp] => 1383634959 ))</required></required>

还是那样,连错误信息都不给全!

  1. $fGetSettings = function($oSettingDaemon) { //处理回调返回的系统设置数据模型 echo $oSettingDaemon;};$EventStack = new EventStack;//请求数据原型$EventStack->addEvent( "DATA_REQUEST", serialize(array( array( "request" => "settingDaemon" ) )), $fGetSettings, null);class EventStack { 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) {print_r(func_get_args());//传入正确 /*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() );print_r($this->_aEventStack);//赋值正确 $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) { //移除指定的观察者 }}
  1. Array( [0] => DATA_REQUEST [1] => a:1:{i:0;a:1:{s:7:"request";s:13:"settingDaemon";}} [2] => Closure Object ( [parameter] => Array ( [$oSettingDaemon] => <required> ) ) [3] => )Array( [0] => Array ( [stamp] => DATA_REQUEST [value] => a:1:{i:0;a:1:{s:7:"request";s:13:"settingDaemon";}} [handler] => Closure Object ( [parameter] => Array ( [$oSettingDaemon] => <required> ) ) [scope] => [timestamp] => 1383634959 ))</required></required>



板大。您好,我把所有代码都贴出来,您就帮我解决下吧。

文件:app/welcome.php

  1. <!--?php//边界判定if (!defined("__IS_ROOT")) die("Access Denied");if (!defined("__IS_APP_ENABLE")) die("Access Denied");global $EventStack, $Smarty;$oSettingDaemon = null;$oArchiveList_Case = null;//闭包函数(回调)$fGetSettings = function() use (&$oSettingDaemon) {
  2. //处理回调返回的系统设置数据模型
  3. echo "wow";};$fDataMap = function() use (&$oArchiveList_Case) {
  4. //处理回调返回的数据模型中的数据地图
  5. //模板赋值
  6. echo "dota";};//请求数据原型$EventStack--->addEvent(//添加数据请求至事件堆栈
  7. "DATA_REQUEST",
  8. serialize(array(
  9. array(
  10. "request" => "settingDaemon"
  11. )
  12. )),
  13. $fGetSettings,
  14. null);$EventStack->addEvent(//添加数据请求至事件堆栈
  15. "DATA_REQUEST",
  16. serialize(array(
  17. array(
  18. "request" => "archiveList",
  19. "index" => "case",
  20. "sort" => "date DESC",
  21. "limit" => "0, 8"
  22. ),
  23. array(
  24. "request" => "archiveList",
  25. "index" => "case",
  26. "sort" => "successful_flag DESC",
  27. "limit" => "0, 8"
  28. ),
  29. array(
  30. "request" => "archiveList",
  31. "index" => "detection",
  32. "sort" => "hot_flag DESC",
  33. "limit" => "0, 5"
  34. ),
  35. array(
  36. "request" => "archiveList",
  37. "index" => "news",
  38. "sort" => "date DESC",
  39. "limit" => "0, 2"
  40. ),
  41. array(
  42. "request" => "archiveList",
  43. "index" => "news_industrial",
  44. "sort" => "hot_flag DESC",
  45. "limit" => "0, 1"
  46. ),
  47. array(
  48. "request" => "userList",
  49. "level" => "worker"
  50. ),
  51. array(
  52. "request" => "archiveList",
  53. "index" => "news",
  54. "sort" => "count_clicks DESC",
  55. "limit" => "0, 3"
  56. )
  57. )),
  58. $fDataMap,//指定回调函数
  59. null//指明上下文环境);//为模版绑定数据//
输出页面$Smarty->display("kit-full.tpl");?>


文件: EventStack.php

  1. <!--?php//事件堆栈处理组件/* * 堆栈程序应当只负责本职工作,即如实的记录和管理事件堆栈 * 堆栈程序对外界应当仅提供 添加观察者,移除观察者,添加事件,清空事件,获取堆栈列表或内容的接口 * 堆栈程序不应当负责对外的逻辑处理 * 通过两种方式主动向外界通讯 * 观察者:当有观察者(通常是个对象)指定的事件发生时,主动调用该组件的__invoke函数 * 回调函数,当添加事件时,事件的添加者可指定一个回调函数,当观察者对此事件做出处理之后,将会激活此回调函数 *///已知的事件戳记/* * NOTICE, 系统运行时的提示消息 * WARNING, 系统运行时的警告级消息 * ERROR, 系统运行时的错误级消息 * ISSUE_TRACK, 由错误处理和安全组件注册的戳记 * DATA_REQUEST, 由数据模型组件注册的戳记 */if (!defined("__IS_ROOT")) die("Access Denied");class EventStack extends Init{
  2. private $_aWatcherRegistry = array();//已注册的观察者列表
  3. private $_aEventStack = array();//事件堆栈
  4. private $_aCallbackRegistry = array();//已注册的回调函数
  5. function __construct() {
  6. $this--->addEvent(
  7. "NOTICE",
  8. serialize(array("Module(Core) and Module(Pdo) is initiated"))
  9. );
  10. $this->addEvent(
  11. "NOTICE",
  12. serialize(array("Module(EventStack) is initiated"))
  13. );
  14. }
  15. function __destruct() {
  16. //将关键信息存储至数据源的Log表
  17. //根据调试开关,决定是否
输出调试信息至页面 $bDebugLock = parent::getArgument("lockring", "debug"); if ($bDebugLock) { $this->exportEventStack(); } } 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->toast($iKey); return $iKey; } public function addWatcher($oWatcher, $sWatchStamp) { //添加观察者 if(is_object($oWatcher)) { $this->_aWatcherRegistry[$sWatchStamp] = $oWatcher; $this->addEvent( "NOTICE", serialize(array("Watcher($sWatchStamp) is added on EventStack")) ); } } ##C## public function clearEventStack() { //清空事件堆栈 unset($this->_aEventStack); $this->addEvent( "NOTICE", serialize(array("EventStack is cleared")) ); } ##E## public function exportEventStack() { //输出调试信息 echo "debug"; echo "_________________________________________________>
"; foreach ($this->_aEventStack as $key => $value) { //if (in_array($value["stamp"], array("NOTICE", "WARNING", "ERROR", "ISSUE_TRACK"))) { echo "Stackid: $key Stamp ".$value["stamp"]." Backtrace ".$value["value"]." TIMESTAMP ".date("h:m:s",$value["timestamp"])."
"; //} } } ##G## public function getStack($iStackId) { //根据是否提供堆栈序号,返回堆栈列表或指定堆栈的内容 if ($iStackId) { if(!isset($_aEventStack[$iStackId])) { $this->addEvent( "ISSUE_TRACK", serialize(array( "sLevel" => "notice", "sLocate" => "", "sMessage" => "Requested stack id($iStackId) doesn't existed in EventStack", "isLog" => true, "bDirectOutput" => false )) ); return null; } else { return $_aEventStack[$iStackId]; } } else { return $this->_aEventStack; } } ##L## public function loopEvent() { //遍历事件堆栈 //在所有其他组件运行完成时执行 #如果发现需要回调的或被观察者绑定的事件,则作出具体动作 foreach ($this->_aEventStack as $aEvent) { //推送相应事件至绑定此类型事件的观察者 if (array_key_exists($aEvent["stamp"], $this->_aWatcherRegistry)) { $mCallback = $this->_aWatcherRegistry[$aEvent["stamp"]] ( $aEvent["stamp"], $aEvent["value"] ); } //检查回调函数 $fFunc = $aEvent["fHandler"]; if (isset($mCallback) && is_callable($fFunc)) { $fFunc($mCallback); } else { } } } ##N## private function toast($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"] ); //如果指定了回调函数,依据观察者返回的数据,做出具体的操作 if (isset($this->_aEventStack[$iKey]["fHandler"]) && !$mCallback) { echo $func = $this->_aEventStack[$iKey]["fHandler"]; echo 1; $func($mCallback); } else { echo 2; $func(null); } } } ##R## public function removeWatcher($sWatchStamp) { //移除指定的观察者 unset($this->_aWatcherRegistry[$sWatchStamp]); if (empty($this->_aWatcherRegistry[$sWatchStamp])) { $this->addEvent( "NOTICE", serialize(array("Watcher($sWatchStamp) is removed")) ); return true; } else { return false; } }}?>

出错信息:

Fatal error: Function name must be a string in C:\wamp\www\newzhebo\module\EventStack.php on line 157

Call Stack
# Time Memory Function Location
1 0.0000 146864 {main}( ) ..\index.php:0
2 0.0540 1074504 Init->start( ) ..\index.php:57
3 0.0540 1074520 App->__invoke( ) ..\Init.php:152
4 0.0550 1083208 require_once( 'C:\wamp\www\newzhebo\module\app\welcome.php' ) ..\App.php:30
5 0.0550 1084664 EventStack->addEvent( ) ..\welcome.php:34
6 0.0550 1085176 EventStack->toast( ) ..\EventStack.php:65


文件:Model.php

  1. <!--?php//数据模型组件if (!defined("__IS_ROOT")) die("Access Denied");interface iModeldo{
  2. function __toString();//将数据模型的数据转化为字符串返回以方便传递
  3. function onModel($sModel, $aArgPack);//生成数据模型
  4. function onModify($sModel, $aSqlPack);//修改数据模型}class SettingDaemon{
  5. //[系统运行时设置]数据模型
  6. #variables
  7. public $aSettingDaemon;
  8. #functions
  9. function __toString() {
  10. $aObjectArray = array(
  11. "asettingdaemon" =--> $this->aSettingDaemon
  12. );
  13. print_r($aObjectArray);
  14. }
  15. function onModel($sModel, $aArgPack) {
  16. if ($sModel != "settingdaemon") return false;
  17. global $Core, $EventStack, $Pdo;
  18. try {
  19. $aDataBaseSoftLink = $Core->getArgument("db", "softLink");
  20. $sTable = $aDataBaseSoftLink["system"];
  21. $sSql = "SELECT * FROM $sTable";
  22. $aResult = $Pdo->query($sSql);
  23. $sSettingDaemon = $aResult->fetchColumn();
  24. $this->aSettingDaemon = unserialize($sSettingDaemon);
  25. //返回数据模型
  26. return $this;
  27. } catch(PDOException $e) {
  28. $EventStack->addEvent(
  29. "ISSUE_TRACK",
  30. serialize(array(
  31. "sLevel" => "ERROR",
  32. "sLocate" => "Model->on->SettingDaemon",
  33. "sMessage" => "Requested Model(SettingDaemon) failed, possibly reason:".$e->getMessage(),
  34. "isLog" => true,
  35. "bDirectOutput" => false
  36. ))
  37. );
  38. return false;
  39. }
  40. }
  41. function onModify($sModel, $aSqlPack) {
  42. if ($sModel != "settingdaemon") return false;
  43. global $EventStack, $Pdo;
  44. try {
  45. $aDataBaseSoftLink = $Core->getArgument("db", "softlink");
  46. $sTable = $aDataBaseSoftLink["system"];
  47. foreach ($aSqlPack as $sSql) {
  48. $Pdo->query($sSql);
  49. }
  50. } catch(PDOException $e) {
  51. $EventStack->addEvent(
  52. "ISSUE_TRACK",
  53. serialize(array(
  54. "sLevel" => "ERROR",
  55. "sLocate" => "Model->modify->SettingDaemon",
  56. "sMessage" => "",
  57. "isLog" => true,
  58. "bDirectOutput" => false
  59. ))
  60. );
  61. }
  62. }}class StaticWords{
  63. //[静态文本]数据模型
  64. #variables
  65. public $sWelcomeIntro;
  66. public $sAboutZhebo;
  67. public $sAboutCulture;
  68. public $sAboutLaboratory;
  69. public $sAboutTeam;
  70. public $sAboutHornor;
  71. public $sFootIntro;
  72. #functions
  73. function __toString() {
  74. }
  75. function onModel($sModel, $aArgPack) {
  76. if ($sModel != "staticwords") return false;
  77. global $Core, $EventStack, $Pdo;
  78. try {
  79. $aDataBaseSoftLink = $Core->getArgument("db", "softLink");
  80. $sTable = $aDataBaseSoftLink["system"];
  81. $sSql = "SELECT * FROM $sTable";
  82. $aResult = $Pdo->query($sSql);
  83. $aResultLine = $aResult->fetch();
  84. $this->sWelcomeIntro = $aResultLine("introduction_short");
  85. $this->sAboutZhebo = $aResultLine("introduction_all");
  86. $this->sAboutCulture = $aResultLine("introduction_culture");
  87. $this->sAboutLaboratory = $aResultLine("introduction_lab");
  88. $this->sAboutTeam = $aResultLine("introduction_team");
  89. $this->sAboutHornor = $aResultLine("introduction_hornor");
  90. $this->sFootIntro = $aResultLine("introduction_foot");
  91. //返回数据模型
  92. return $this;
  93. } catch(PDOException $e) {
  94. $EventStack->addEvent(
  95. "ISSUE_TRACK",
  96. serialize(array(
  97. "sLevel" => "ERROR",
  98. "sLocate" => "Model->on->SettingDaemon",
  99. "sMessage" => "Requested Model(SettingDaemon) failed, possibly reason:".$e->getMessage(),
  100. "isLog" => true,
  101. "bDirectOutput" => false
  102. ))
  103. );
  104. return false;
  105. }
  106. }
  107. function onModify($sModel, $aSqlPack) {
  108. if ($sModel != "staticwords") return false;
  109. global $EventStack, $Pdo;
  110. try {
  111. //取得所需表名
  112. $aDataBaseSoftLink = $Core->getArgument("db", "softlink");
  113. $sTable = $aDataBaseSoftLink["system"];
  114. //验证参数
  115. //生成Sql查询语句
  116. //更新数据
  117. //返回对象
  118. return $this;
  119. } catch(PDOException $e) {
  120. $EventStack->addEvent(
  121. "ISSUE_TRACK",
  122. serialize(array(
  123. "sLevel" => "ERROR",
  124. "sLocate" => "Model->modify->SettingDaemon",
  125. "sMessage" => "",
  126. "isLog" => true,
  127. "bDirectOutput" => false
  128. ))
  129. );
  130. }
  131. }}class ArchiveContent implements iModeldo{
  132. //[文章内容]数据模型
  133. #variables
  134. public $iId;
  135. public $sIndex;
  136. public $iAuthor;
  137. public $sAuthorName;
  138. public $sTimestamp;
  139. public $sTitle;
  140. public $sContent;
  141. public $bHotFlag;
  142. public $iCountClicks;
  143. #functions
  144. function __toString() {
  145. }
  146. function onModel($sModel, $aArgPack) {
  147. if ($sModel != "archivecontent") return false;
  148. global $Core, $EventStack;
  149. extract($aArgPack);
  150. try {
  151. //获得所需的数据表名
  152. $aDataBaseSoftLink = $Core->getArgument("db", "softLink");
  153. $sTable = $aDataBaseSoftLink["archive"];
  154. //核实参数
  155. $sSqlId = isset($iId)? " WHERE `id`=$iId": " WHERE `id`=1";
  156. //生成查询Sql
  157. $sSql = "SELECT * FROM $sTable".$sSqlId;
  158. //取得数据
  159. $aResult = $Pdo->query($sSql);
  160. while ($aRow = $aResult->fetch()) {
  161. $this->aArchiveList[] = array(
  162. "id" => $aRow["id"],
  163. "index" => $aRow["index"],
  164. "date" => $aRow["date"],
  165. "author" => $aRow["author"],
  166. "title" => $aRow["title"],
  167. "content" => $aRow["content"],
  168. "hot_flag" => $aRow["hot_flag"],
  169. "successful_flag" => $aRow["successful_flag"],
  170. "count_clicks" => $aRow["count_clicks"]
  171. );
  172. }
  173. //返回数据模型
  174. return $this;
  175. } catch(PDOException $e) {
  176. $EventStack->addEvent(
  177. "ERROR",
  178. serialize(array(
  179. "sLevel" => "ERROR",
  180. "sLocate" => "Model->on->ArchiveContent",
  181. "sMessage" => "Requested Model(ArchiveContent) failed, possibly reason:".$e->getMessage(),
  182. "isLog" => true,
  183. "bDirectOutput" => false
  184. ))
  185. );
  186. }
  187. }
  188. function onModify($sModel, $aSqlPack) {
  189. if ($sModel != "archivecontent") return false;
  190. }}class Model extends Init{
  191. #variables
  192. private $_aModelMap = array();//戳记和类名的实际对应
  193. private $_aModels = array();//已注册的数据模型模块
  194. #functions
  195. function __construct() {
  196. $this->registry();
  197. //添加所有的数据模块至数据模块列表
  198. $this->registerModel("SettingDaemon");
  199. $this->registerModel("StaticWords");
  200. $this->registerModel("ArchiveContent");
  201. $this->registerModel("ArchiveList");
  202. $this->registerModel("UserDetail");
  203. $this->registerModel("UserList");
  204. $this->registerModel("FriendLinkList");
  205. $this->registerModel("ChattingLine");
  206. $this->registerModel("SearchResult");
  207. }
  208. function __invoke($sStamp, $sValue) {
  209. //收到事件堆栈推送的消息
  210. if ($sStamp != "DATA_REQUEST") {
  211. //复查消息戳记
  212. die("Module(Model) recived wrong toast from EventStack");
  213. } else {
  214. $aValue = unserialize($sValue);
  215. //核查参数
  216. if (!is_array($aValue)) {
  217. //数据请求者传递?本组件的参数不合法
  218. } else {
  219. foreach ($aValue as $aArgPack) {
  220. //获得数据模型名
  221. $sModelName = strtolower($aArgPack["request"]);
  222. //提取传递给数据模型的参数列表
  223. array_shift($aArgPack);
  224. //生成需要的数据模型对象
  225. $oModel = $this->creatModel($sModelName);
  226. //添加次数据模型对象至保存的列表
  227. $this->_aModels[] = $oModel;
  228. //继续该数据模型填充数据,并保存至要返回的数据模型列表中
  229. $oModel->onModel($sModelName, $aArgPack);
  230. }
  231. //将所有数据模型对象作为参数返回至事件堆栈, 事件堆栈会视回调函数返回至数据请求方,注意是一个数组
  232. return $this->_aModels;
  233. }
  234. }
  235. }
  236. ##R##
  237. function registry() {
  238. //注册此组件为事件堆栈组件的观察者
  239. global $EventStack;
  240. $EventStack->addWatcher($this, "DATA_REQUEST");
  241. $EventStack->addWatcher($this, "DATA_MODIFY");
  242. }
  243. private function registerModel($sModelName) {
  244. //注册戳记和类名的对应关系
  245. $this->_aModelMap[strtolower($sModelName)] = $sModelName;
  246. }
  247. private function creatModel($sModelName) {
  248. //以工厂模式生成数据模型
  249. if (in_array($sModelName, array_keys($this->_aModelMap))) {
  250. $sClassName = $this->_aModelMap[$sModelName];
  251. return new $sClassName();
  252. } else {
  253. return null;
  254. }
  255. }}?>

文件:App.php

  1. <!--?php//应用程序组件/* * 和前端挂钩的组件 * 从Router组件获得基本的请求信息后 * 1.加载框架,获得框架的数据原型并绑定 * 2.加载具体的页面组件,该页面组件将自行请求数据源型并绑定 * 3.显示合成并处理后的页面 */if(!defined("__IS_ROOT")) die("Access Denied");class App extends Init{
  2. #variables
  3. private $_sApp;//指定具体的应用组件
  4. #functions
  5. function __construct() {
  6. define("__IS_APP_ENABLE", true);
  7. }
  8. function __invoke() {
  9. //通过使用$App()即可完成页面显示
  10. global $Router;
  11. $aAppAvailableList = parent::getArgument("app");
  12. $sApp = $Router--->getController();
  13. if (in_array($sApp, $aAppAvailableList)) {
  14. $this->_sApp = $sApp;
  15. $sFilePath = "module/app/".$sApp.".php";
  16. if (is_file($sFilePath) && is_readable($sFilePath)) {
  17. require_once($sFilePath);
  18. }
  19. }
  20. }}?>

你给全错误信息不经行了吗?
Fatal error: Function name must be a string in C:\wamp\www\newzhebo\module\ EventStack.php on line 157
不就是 EventStack.php 的 157 行出错吗?
EventStack.php

  1. 143 private function toast($iKey) {144 //推送事件至相应的观察者145 if (array_key_exists($this->_aEventStack[$iKey]["stamp"], $this->_aWatcherRegistry)) {146 $mCallback = $this->_aWatcherRegistry[$this->_aEventStack[$iKey]["stamp"]](147 $this->_aEventStack[$iKey]["stamp"],148 $this->_aEventStack[$iKey]["value"]149 );150 //如果指定了回调函数,依据观察者返回的数据,做出具体的操作151 if (isset($this->_aEventStack[$iKey]["fHandler"]) && !$mCallback) {152 echo $func = $this->_aEventStack[$iKey]["fHandler"];153 echo 1;154 $func($mCallback);155 } else {156 echo 2;157 $func(null); //这个 $func 在哪里赋值的? } } }

154 的是在 152 赋值的,哪 157 的 $func 在哪里赋值的?

至少要把 $func = $this->_aEventStack[$iKey]["fHandler"]; 放到条件分支外面吧?

你给全错误信息不经行了吗?
Fatal error: Function name must be a string in C:\wamp\www\newzhebo\module\ EventStack.php on line 157
不就是 EventStack.php 的 157 行出错吗?
EventStack.php

  1. 143 private function toast($iKey) {144 //推送事件至相应的观察者145 if (array_key_exists($this->_aEventStack[$iKey]["stamp"], $this->_aWatcherRegistry)) {146 $mCallback = $this->_aWatcherRegistry[$this->_aEventStack[$iKey]["stamp"]](147 $this->_aEventStack[$iKey]["stamp"],148 $this->_aEventStack[$iKey]["value"]149 );150 //如果指定了回调函数,依据观察者返回的数据,做出具体的操作151 if (isset($this->_aEventStack[$iKey]["fHandler"]) && !$mCallback) {152 echo $func = $this->_aEventStack[$iKey]["fHandler"];153 echo 1;154 $func($mCallback);155 } else {156 echo 2;157 $func(null); //这个 $func 在哪里赋值的? } } }

154 的是在 152 赋值的,哪 157 的 $func 在哪里赋值的?

至少要把 $func = $this->_aEventStack[$iKey]["fHandler"]; 放到条件分支外面吧?


我把代码改成这样,还是上面的错误。

  1. private function toast($iKey) {
  2. //推送事件至相应的观察者
  3. if (array_key_exists($this->_aEventStack[$iKey]["stamp"], $this->_aWatcherRegistry)) {
  4. $mCallback = $this->_aWatcherRegistry[$this->_aEventStack[$iKey]["stamp"]](
  5. $this->_aEventStack[$iKey]["stamp"],
  6. $this->_aEventStack[$iKey]["value"]
  7. );
  8. //如果指定了回调函数,依据观察者返回的数据,做出具体的操作
  9. $func = $this->_aEventStack[$iKey]["fHandler"];
  10. if (isset($this->_aEventStack[$iKey]["fHandler"]) && !$mCallback) {
  11. echo 1;
  12. $func($mCallback);
  13. } else {
  14. echo 2;
  15. $func(null);
  16. }
  17. }
  18. }

var_dump EventStack对象实例的结果如下

object(EventStack)[5]
private '_aWatcherRegistry' =>
array (size=3)
'ISSUE_TRACK' =>
object(Issue)[6]
private '_aLastIssue' =>
array (size=0)
...
'DATA_REQUEST' =>
object(Model)[8]
private '_aModelMap' =>
array (size=9)
...
private '_aModels' =>
array (size=8)
...
protected 'aInitArguments' =>
array (size=6)
...
protected 'aModules' =>
array (size=0)
...
private '_aModuleList' (Init) =>
array (size=11)
...
'DATA_MODIFY' =>
object(Model)[8]
private '_aModelMap' =>
array (size=9)
...
private '_aModels' =>
array (size=8)
...
protected 'aInitArguments' =>
array (size=6)
...
protected 'aModules' =>
array (size=0)
...
private '_aModuleList' (Init) =>
array (size=11)
...
private '_aEventStack' =>
array (size=17)
0 =>
array (size=5)
'stamp' => string 'NOTICE' (length=6)
'value' => string 'a:1:{i:0;s:41:"Module(Core) and Module(Pdo) is initiated";}' (length=59)
'handler' => null
'scope' => null
'timestamp' => int 1383656323
1 =>
array (size=5)
'stamp' => string 'NOTICE' (length=6)
'value' => string 'a:1:{i:0;s:31:"Module(EventStack) is initiated";}' (length=49)
'handler' => null
'scope' => null
'timestamp' => int 1383656323
2 =>
array (size=5)
'stamp' => string 'NOTICE' (length=6)
'value' => string 'a:1:{i:0;s:43:"Watcher(ISSUE_TRACK) is added on EventStack";}' (length=61)
'handler' => null
'scope' => null
'timestamp' => int 1383656323
3 =>
array (size=5)
'stamp' => string 'NOTICE' (length=6)
'value' => string 'a:1:{i:0;s:26:"Module(Issue) is initiated";}' (length=44)
'handler' => null
'scope' => null
'timestamp' => int 1383656323
4 =>
array (size=5)
'stamp' => string 'NOTICE' (length=6)
'value' => string 'a:1:{i:0;s:29:"Module(HashUtls) is initiated";}' (length=47)
'handler' => null
'scope' => null
'timestamp' => int 1383656323
5 =>
array (size=5)
'stamp' => string 'NOTICE' (length=6)
'value' => string 'a:1:{i:0;s:44:"Watcher(DATA_REQUEST) is added on EventStack";}' (length=62)
'handler' => null
'scope' => null
'timestamp' => int 1383656323
6 =>
array (size=5)

人气教程排行