phpseclib:实例化New_SFTP对象时如何正常处理连接错误

时间:2013-06-04 19:35:26

标签: php error-handling sftp phpseclib

我试图以比默认API更优雅的方式处理此连接错误。默认的API会打印一条错误信息,但是我想压制它并用我自己的方式替换它,识别错误并处理它,但我不知道该怎么做。

public function __construct($host)
{
      // trying to test by opening a failed connection -- try catch
      // block doesn't work because no exception thrown
      $this->connection = new Net_SFTP("nonexistent.domain.com");

      // this will not work either ....
      if ( !$this->connection->status_codes )
      {
         throw new Exception("Could not create SFTP connection.\n");
      }

      else
      {
         echo "Connected via SFTP to " . $host . " on port 22.\n";
      }
}

执行它会显示,即使出现错误,它也会触及else块:

PHP Notice:  Cannot connect to nonexistent.domain.com. Error 0. php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/dylan/Documents/php/Net/SSH2.php on line 788
Connected via SFTP to gluon.exavault.com on port 22.

我一直在查看Net/SFTP.php文件中的类,但我似乎无法找到处理错误代码并忽略错误消息的方法。有没有人对另一种可能的解决方案有任何想法?

1 个答案:

答案 0 :(得分:1)

抱歉,误读。

您可以设置error_reporting(E_ALL ^ E_NOTICE)