有些人试图用php解决问题


Some try catch issue with php

我正在尝试创建一个类来使用php中的crontab。我使用了这个教程

我已经安装了libssh2,但正如您所看到的,它还没有工作。因此,我的服务器上有一个文件Ssh2_crontab_manager.php。内容如下:

<?php
      Class Ssh2_crontab_manager 
      {
            private $connection;
            private $path;
            private $handle;
            private $cron_file;
            function __construct($host=NULL, $port=NULL, $username=NULL, $password=NULL)
            {
                $path_length    = strrpos(__FILE__, "/");
                $this->path      = substr(__FILE__, 0, $path_length) . '/';
                $this->handle    = 'crontab.txt';
                $this->cron_file = "{$this->path}{$this->handle}";
                /*try
                {
                    if ((is_null($host)) || (is_null($port)) || (is_null($username)) || (is_null($password))) throw new Exception("Please specify the host, port, username and password!");
                }
                catch
                {
                }*/
            }

      }
?>

这里是noReplyCrontab.php,我尝试在这里使用这个类:

<?php
include './lib/Ssh2_crontab_manager.php';
//$crontab = new Ssh2_crontab_manager('host', '22', 'user', 'pass');
echo 'WORKS';
?>

如果我现在运行它,它会说"有效",但如果我取消对try/catch块的注释,它只显示白色屏幕,所以我认为存在一些错误。有人能给我看吗?

您的代码显示

 catch
 {
 }

但是catch什么?

您必须将该值提供给catch子句

catch (Exception $e)
{
         //now it will work fine
}

手动

尝试这个

try
{
    if (true) throw new Exception("Please specify the host, port, username and password!");
}
catch(Exception $e)
{
    echo $e->getMessage();
}