2012-10-24 59 views
1

我有3个类名 “控制器”, “装载机”, “错误 ”和“ Ex_controller”。如何知道在php中调用函数的位置?

Controller.php这样

class Controller 
{ 
    function __Construct() 
    { 
      $this->load = Loader::getinstant(); 
      $this->error = $this->load->class('Error'); 
    } 
} 

Loader.php

class Loader 
{ 
    function class($class) 
    { 
      require_once($class); 
      return new $class; 
    } 
} 

Error.php

class Error 
{ 
    function query($query) 
    { 
      $res = mysql_query($query) 
      if($res) 
      { 
        return $res; 
      }else{ 
        die('Could not execute query: '.mysql_error().'at line '. __LINE__ . 
         ' in file ' . __FILE__);//does it work?If it doesn't, how to 
               make it work? 
      } 
    } 
} 

Ex_controller.php

class Ex_controller extends Controller 
{ 
    function __Construct() 
    { 
      parent::__construct(); 
      $result = $this->error->query('some sql query');//(*) 
    } 
} 

我如何能显示那里发生的错误Ex_controller有(*)?

+0

为什么'Error'类有一个'query()'方法?这似乎没有道理。 –

回答

1

首先,您应该停止使用mysql_xxx函数,因为它们正在废弃旧API。

除此之外,在这种情况下,开始使用例外而不是普通的旧die()可能是值得的。

if($res) { 
    return $res; 
}else{ 
    throw new Exception("Could not execute query '$query': " . mysql_error()); 
} 

然后控制器内:

try { 
    $result = $this->error->query('some sql query');//(*) 
} catch (Exception $e) { 
    die(print_r($e, true)); // do something more useful with the exception though. 
} 

当你捕捉到了异常高了,你会print_r()它,你会看到文件,行号和一切的完整堆栈跟踪。

另外,您还有机会处理错误。

Plus plus,如果您使用PDO并启用异常错误处理,您甚至不必再自己抛出异常。

+0

感谢兄弟!但是当我使用它时,localhost返回HTTP 500(内部服务器错误)。它的日志似乎没有什么错在这里发生!我该如何解决它? –

+0

@quanganh_developer你必须'尝试{}抓住'当然是例外:) –

+0

哦,我的上帝!我的错!谢谢:D! –

1

在你的类Ex_controller扩展控制器 __construct上的第一行添加

parent::__construct(); 

()函数

如何过错误类应该只返回错误,不进行查询....

+0

谢谢你提示我! –

相关问题