There are questions remain, We'll search for the answers together. But one thing we known for sure,the future is not set!

【原创文章】一个在wordpress中安装DW Question Answer插件出现致命错误的解决办法

wordpress 百蔬君 8629℃ 已收录 0评论

这几天折腾wordpres的一些插件,其中在安装好几个插件的时候都提示出现致命错误,导致插件无法启动。
我安装启动DW Question Answer问答系统,或者AnsPress社群问答系统都出现错误信息,
提示信息为

无法启用插件,因为它引起了一个致命错误(fatal error)。
Parse error: syntax error, unexpected T_FUNCTION, expecting ')' in ...\wp-content\plugins\dw-question-answer\inc\autoload.php on line 4

Snap1

查看这些出现致命问题的文件的源码

spl_autoload_register(
	 function ($className) {
		$classPath = explode('_', $className);

		if ( $classPath[0] != 'DWQA' ) {
			return;
		}
		// Drop 'Google', and maximum class file path depth in this project is 3.
		$classPathSlice = array_slice($classPath, 1, 2);
		if ( count( $classPath ) > 3 ) {
			for ($i=3; $i < count( $classPath ); $i++) { 
				$classPathSlice[1] .= '_' . $classPath[$i];
			}
		}
		$filePath = DWQA_DIR . 'inc/' . implode('_', $classPathSlice) . '.php';
		if ( ! file_exists( $filePath ) ) {
			$filePath = DWQA_DIR . 'inc/' . implode('/', $classPathSlice) . '.php';
		}
		if (file_exists($filePath)) {
			require_once($filePath);
		}
	}
);  

可以看到这是一个自动加载注册函数,当PHP碰到没有定义的类就会激活这个自动加载注册函数。

从代码中可以看到这个代码的写法使用的是php5.3中才支持的匿名函数的写法,如果php环境是5.3以下的版本就会无法识别,出现致命错误!

如果是低于5.3版本的php系统,那么这个代码可以简单修改一下,我的办法如下

function loadprint($className) {
		$classPath = explode('_', $className);

		if ( $classPath[0] != 'DWQA' ) {
			return;
		}
		// Drop 'Google', and maximum class file path depth in this project is 3.
		$classPathSlice = array_slice($classPath, 1, 2);
		if ( count( $classPath ) > 3 ) {
			for ($i=3; $i < count( $classPath ); $i++) { 
				$classPathSlice[1] .= '_' . $classPath[$i];
			}
		}
		$filePath = DWQA_DIR . 'inc/' . implode('_', $classPathSlice) . '.php';
		if ( ! file_exists( $filePath ) ) {
			$filePath = DWQA_DIR . 'inc/' . implode('/', $classPathSlice) . '.php';
		}
		if (file_exists($filePath)) {
			require_once($filePath);
		}
	}
spl_autoload_register('loadprint');  

这样插件就可以安装启动成功了。
ffggg1

转载请注明:百蔬君 » 【原创文章】一个在wordpress中安装DW Question Answer插件出现致命错误的解决办法

喜欢 (0)or分享 (0)
发表我的评论
取消评论

请证明您不是机器人(^v^):

表情