Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 父类don';t捕获子类中重写方法的异常_Php_Oop_Exception_Inheritance_Slim - Fatal编程技术网

Php 父类don';t捕获子类中重写方法的异常

Php 父类don';t捕获子类中重写方法的异常,php,oop,exception,inheritance,slim,Php,Oop,Exception,Inheritance,Slim,我有这个问题。我有一个具有render方法的类模板,我希望Slim能够呈现这个类的对象,如果它们是由路由处理程序返回的 $app->get('/test',function(){ 返回新模板('main',function()用法($error){ 返回数组( “内容”=>“您好” ); }); }); 它是我创建的子类(在System.php中) 类系统扩展Slim{ 函数_构造函数(){ Slim::_构造(); } 私有函数自动渲染乐趣($callable){ $app=$this; 返

我有这个问题。我有一个具有render方法的类模板,我希望Slim能够呈现这个类的对象,如果它们是由路由处理程序返回的

$app->get('/test',function(){
返回新模板('main',function()用法($error){
返回数组(
“内容”=>“您好”
);
});
});
它是我创建的子类(在System.php中)

类系统扩展Slim{
函数_构造函数(){
Slim::_构造();
}
私有函数自动渲染乐趣($callable){
$app=$this;
返回函数()使用($callable,$app){
$args=func_get_args();
$ret=call\u user\u func\u数组($callable,$args);
if($ret instanceof Template){
//渲染模板-Slim忽略返回值
$app->response()->body($ret->render());
}
};
}
受保护的函数映射路由($args){
$last=计数($args)-1;
$args[$last]=$this->auto_render_fun($args[$last]);
返回Slim::mapRoute($args);
}
}
我想对notFound做同样的事情

$app->notFound(函数()使用($app){
$response=$app->response();
$response['Content-Type']='text/html';
返回新模板('main',函数(){
返回数组('content'=>新模板('error_404',null));
});
});
所以我重写了notFound函数来包装闭包并呈现它的返回值

首先,我尝试使用更小的代码

未找到公共函数($callable=null){
如果(!is_null($callable))){
$this->router->notFound($this->auto_render_fun($callable));
}否则{
Slim::notFound();
}
}
我也尝试了这个(复制和修改旧代码)

未找到公共函数($callable=null){
如果(!为空($callable)){
$this->router->notFound($this->auto_render_fun($callable));
//$this->router->notFound($callable);//旧行
}否则{
ob_start();
$customNotFoundHandler=$this->router->notFound();
如果(可调用($customNotFoundHandler)){
调用用户函数($customNotFoundHandler);
}否则{
调用用户函数(数组($this,'defaultNotFound');
}
$this->halt(404,ob_get_clean());
}
}
但是它不起作用的原因是它运行了
Slim\u Exception\u Stop
,假设Slim会缓存它。这里是调用
$this->notFound()的代码的那一行
它在里面,试着接住

这里是堆栈跟踪(我将其缓存在notFound函数中,但它应该在Slim类中处理)


好的,我找到了原因,异常得到了应有的处理,但没有内容。我错误地认为这是与类有关的,但简单地说,Slim代码有bug

公共函数暂停($status,$message=''){
$this->cleanBuffer();
$this->response->status($status);
$this->response->body($message);
$this->stop();
}
函数覆盖处理程序中使用的数据,如下所示

$app->notFound(函数(){
$app->response()->body('Error');
});
无法工作,未找到的函数应如下所示

未找到公共函数($callable=null){
如果(!is_null($callable))){
$this->router->notFound($this->auto_render_fun($callable));
}否则{
$customNotFoundHandler=$this->router->notFound();
如果(可调用($customNotFoundHandler)){
调用用户函数($customNotFoundHandler);
}否则{
调用用户函数(数组($this,'defaultNotFound');
}
$this->response->status(404);
$this->stop();
}
}
Slim_Exception_Stop in file libs/Slim/Slim/Slim.php at 862
0: Slim->stop() in libs/Slim/Slim/Slim.php at 882
1: Slim->halt(integer, string) in libs/System.php at 187
2: System->notFound() in libs/Slim/Slim/Slim.php at 1161
3: Slim->call() in libs/Slim/Slim/Middleware/Flash.php at 84
4: Slim_Middleware_Flash->call() in libs/Slim/Slim/Middleware/MethodOverride.php at 91
5: Slim_Middleware_MethodOverride->call() in libs/Slim/Slim/Middleware/PrettyExceptions.php at 65
6: Slim_Middleware_PrettyExceptions->call() in libs/Slim/Slim/Middleware/PrettyExceptions.php at 65
7: Slim_Middleware_PrettyExceptions->call() in libs/Slim/Slim/Slim.php at 1098
8: Slim->run() in index.php at 573