扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:haohappy2004 来源:BLOG 2007年10月21日
关键字: Linux
<?php // PHP 5 require_once('cmd_php5/Command.php'); class CommandManager { private $cmdDir = "cmd_php5"; function __construct() { if (!is_dir($this->cmdDir)) { throw new Exception("directory error: $this->cmdDir"); } } function getCommandObject($cmd) { $path = "{$this->cmdDir}/{$cmd}.php"; if (!file_exists($path)) { throw new Exception("Cannot find $path"); } require_once $path; if (!class_exists($cmd)) { throw new Exception("class $cmd does not exist"); } $class = new ReflectionClass($cmd); if (!$class->isSubclassOf(new ReflectionClass('Command'))) { throw new Exception("$cmd is not a Command"); } return new $cmd(); } } ?> |
<?php // PHP 5 try { $mgr = new CommandManager(); // potential error $cmd = $mgr->getCommandObject('realcommand'); // another potential error $cmd->execute(); } catch (Exception $e) { // handle either error here print $e->getMessage(); exit(); } ?> |
<?php // PHP 5 require_once('cmd_php5/Command.php'); class CommandManager { private $cmdDir = "cmd_php5"; const CMDMAN_GENERAL_ERROR = 1; const CMDMAN_ILLEGALCLASS_ERROR = 2; function __construct() { if (!is_dir($this->cmdDir)) { throw new Exception("directory error: $this->cmdDir", self::CMDMAN_GENERAL_ERROR); } } function getCommandObject($cmd) { $path = "{$this->cmdDir}/{$cmd}.php"; if (!file_exists($path)) { throw new Exception("Cannot find $path", self::CMDMAN_ILLEGALCLASS_ERROR); } require_once $path; if (!class_exists($cmd)) { throw new Exception("class $cmd does not exist", self::CMDMAN_ILLEGALCLASS_ERROR); } $class = new ReflectionClass($cmd); if (!$class->isSubclassOf(new ReflectionClass('Command'))) { throw new Exception("$cmd is not a Command", self::CMDMAN_ILLEGALCLASS_ERROR); } return $class->newInstance(); } } ?> |
<?php // PHP 5 try { $mgr = new CommandManager(); $cmd = $mgr->getCommandObject('realcommand'); $cmd->execute(); } catch (Exception $e) { if ($e->getCode() == CommandManager::CMDMAN_GENERAL_ERROR) { // no way of recovering die($e->getMessage()); } else if ($e->getCode() == CommandManager::CMDMAN_ILLEGALCLASS_ERROR) { error_log($e->getMessage()); print "attempting recovery\n"; // perhaps attempt to invoke a default command? } } ?> |
<?php // PHP 5 require_once('cmd_php5/Command.php'); class CommandManagerException extends Exception{} class IllegalCommandException extends Exception{} class CommandManager { private $cmdDir = "cmd_php5"; function __construct() { if (!is_dir($this->cmdDir)) { throw new CommandManagerException("directory error: $this->cmdDir"); } } function getCommandObject($cmd) { $path = "{$this->cmdDir}/{$cmd}.php"; if (!file_exists($path)) { throw new IllegalCommandException("Cannot find $path"); } require_once $path; if (!class_exists($cmd)) { throw new IllegalCommandException("class $cmd does not exist"); } $class = new ReflectionClass($cmd); if (!$class->isSubclassOf(new ReflectionClass('Command'))) { throw new IllegalCommandException("$cmd is not a Command"); } return $class->newInstance(); } } ?> |
<?php // PHP 5 try { $mgr = new CommandManager(); $cmd = $mgr->getCommandObject('realcommand'); $cmd->execute(); } catch (CommandManagerException $e) { die($e->getMessage()); } catch (IllegalCommandException $e) { error_log($e->getMessage()); print "attempting recovery\n"; // perhaps attempt to invoke a default command? } catch (Exception $e) { print "Unexpected exception\n"; die($e->getMessage()); } ?> |
<?php // PHP 5 try { $mgr = new CommandManager(); $cmd = $mgr->getCommandObject('realcommand'); $cmd->execute(); } catch (Exception $e) { print "Unexpected exception\n"; die($e->getMessage()); } catch (CommandManagerException $e) { die($e->getMessage()); } catch (IllegalCommandException $e) { error_log($e->getMessage()); print "attempting recovery\n"; // perhaps attempt to invoke a default command? } ?> |
<?php // PHP 5 class RequestHelper { private $request = array(); private $defaultcmd = 'defaultcmd'; private $cmdstr; function __construct($request_array=null) { if (!is_array($this->request = $request_array)) { $this->request=$_REQUEST; } } function getCommandString() { return ($this->cmdstr ? $this->cmdstr : ($this->cmdstr=$this->request['cmd'])); } function runCommand() { $cmdstr = $this->getCommandString(); try { $mgr = new CommandManager(); $cmd = $mgr->getCommandObject($cmdstr); $cmd->execute(); } catch (IllegalCommandException $e) { error_log($e->getMessage()); if ($cmdstr != $this->defaultcmd) { $this->cmdstr = $this->defaultcmd; $this->runCommand(); } else { throw $e; } } catch (Exception $e) { throw $e; } } } $helper = new RequestHelper(array(cmd=>'realcommand')); $helper->runCommand(); ?> |
} catch (Exception $e) { throw $e; } |
return $class->newInstance(); |
<?php // PHP 5 class Front { static function main() { try { $helper = new RequestHelper(array(cmd=>'realcommand')); $helper->runCommand(); } catch (Exception $e) { print "<h1>".get_class($e)."</h1>\n"; print "<h2>{$e->getMessage()} ({$e->getCode()})</h2>\n\n"; print "file: {$e->getFile()}<br />\n"; print "line: {$e->getLine()}<br />\n"; print $e->getTraceAsString(); die; } } } Front::main(); ?> |
ReflectionException Access to non-public constructor of class realcommand (0) file: c:\MyWEB\Apache\htdocs\php5exception\index_php5_4.php line: 31 #0 c:\MyWEB\Apache\htdocs\php5exception\index_php5_5.php(25): CommandManager->getCommandObject() #1 c:\MyWEB\Apache\htdocs\php5exception\index_php5_6.php(10): RequestHelper->runCommand('realcommand') #2 c:\MyWEB\Apache\htdocs\php5exception\index_php5_6.php(23): Front::main() #3 {main} |
key | 含义 |
file | 产生异常的文件 |
line | 产生异常的类方法所在行数 |
function | 产生异常的函数/方法 |
class | 调用的方法所在类 |
type | 调用类型:'::' 表示调用静态类成员 '->' 表示实例化调用(先实例化生成对象再调用) |
args | 类方法接受的参数 |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者