简体   繁体   English

致命错误:无法在第194行的/var/www/phpseclib/Net/SSH2.php中重新声明类Net_SSH2

[英]Fatal error: Cannot redeclare class Net_SSH2 in /var/www/phpseclib/Net/SSH2.php on line 194

I have created a CI model that dynamically loads certain classes depending on a parameter that is passed in. These classes are just wrapper classes around phpseclib to make ssh connections to different devices. 我创建了一个CI模型,该模型根据传入的参数动态加载某些类。这些类只是phpseclib的包装器类,用于建立与不同设备的ssh连接。 What I'm noticing is that when I try to execute one particular method, I'm getting the above error message. 我注意到的是,当我尝试执行一种特定方法时,我收到了以上错误消息。

Here's some sample code to help you understand what I'm doing. 这是一些示例代码,可以帮助您了解我在做什么。 This is what my model looks like: 这是我的模型的样子:

    public function get_portstatusall($ip, $switchname)
    {           
        $classname = $this->switchToClassName($switchname);
        try{
        include_once(APPPATH.'libraries/'.$classname.'.php');                 
        $switch_obj = new $classname($ip, 'password', '');
        $switch_obj->connect();
        $data = $switch_obj->dosomething(); 
        $switch_obj->disconnect();  
        return $data;
        }
         catch (Exception $e) {
            echo 'this really should be logged...';
            return false;
         }
    }   

            public function get_portstatusindividual($ip, $switchname)
            {           
                 $classname = $this->switchToClassName($switchname);
                 try{

                     include_once(APPPATH.'libraries/'.$classname.'.php'); 
         $switch_obj = new $classname($ip, 'password', '');
         $switch_obj->connect();
         $data = $switch_obj->dosomethingelse();    
         $switch_obj->disconnect();  
         return $data;
         }
         catch (Exception $e) {
            echo 'this really should be logged...';
            return false;
         }
               }

As you can see, i'm dynamically determining which class to load depending on the switchname that is passed in. This code successfully loads a class called "device123.php", let's say. 如您所见,我正在根据传入的切换名动态确定要加载的类。此代码成功加载了一个名为“ device123.php”的类。 Class device123 in turn, instantiates the SSH2 object that comes with the phpseclib, and uses it to send ssh commands to the device. 依次类device123,实例化phpseclib随附的SSH2对象,并使用该对象将ssh命令发送到设备。

Here's a clip of code from device 123: 这是设备123的代码片段:

class device123
{
// sample code to demo how to use phpseclib to create an interactive ssh session.
//this library relies on phpseclib.  you must include this class and SSH2.php from Net/phpseclib.


private $_hostname;
private $_password;
private $_username;
private $_connection;
private $_data;
private $_timeout;
private $_prompt;

public function __construct($hostname, $password, $username = "", $timeout = 10) 
//public function __construct($params) 
{
    echo 'in the switch constructor<br>';
    set_include_path(get_include_path() . PATH_SEPARATOR . '/var/www/phpseclib');
    include('Net/SSH2.php');

   $this->_hostname = $hostname;
    $this->_password = $password;
    $this->_username = $username;

} // __construct

public function connect() 
{
    $ssh = new Net_SSH2($this->_hostname); 
    if (!$ssh->login($this->_username, $this->_password)) { //if you can't log on...
    die("Error: Authentication Failed for $this->_hostname\n");
    }
    else  {

    $output= $ssh->write("\n"); //press any key to continue prompt;
    $prompt=$ssh->read('/([0-9A-Z\-])*(#)(\s*)/i', NET_SSH2_READ_REGEX); 
    if (!$prompt) {
        die("Error: Problem connecting for $this->_hostname\n");
    }  
    else {

        $this->_connection = $ssh;
    }

    }
} // connect


public function close() 
{
    $this->_send('exit');
} // close

public function disconnect()
{
  $this->_connection->disconnect();
  $ssh=NULL;
}

I don't think I quite understand how I can be redeclaring the SSH2 class... but i wanted to see if perhaps I was not destroying / cleaning up after myself properly. 我认为我不太了解如何重新声明SSH2类...但是我想查看一下是否可能我没有正确地破坏/清理自己。 To help troubleshoot, I've tried adding debug echo statements in the constructor and destructor of both the SSH2 class, and also the wrapper class called device123. 为了帮助进行故障排除,我尝试在SSH2类以及称为device123的包装器类的构造函数和析构函数中添加调试回显语句。 It all looks proper... 看起来都不错...

I don't think I'm on the right track... Can you tell me where you think I should start looking? 我认为我的前进方向不正确...您能告诉我您认为我应该开始寻找什么地方吗? Is it because it's possible that both methods are called ... one after the other... and both can possibly load the same class? 是否因为两个方法都可能被一个接一个地调用而又都可以加载同一个类?

Thanks. 谢谢。

当您尝试多次加载类时,例如在其他源文件中存在相同的名称类,PHP会给出此类错误。

All php classes must have unique names. 所有的php类必须具有唯一的名称。 You should include all the files at one place, keep track of all loaded classes if you want a lazy loading, or just use http://cz.php.net/include_once . 您应该include所有文件放在一个位置,如果要延迟加载,请跟踪所有已加载的类,或者只使用http://cz.php.net/include_once The same goes for the require function. require函数也是如此。

The _once postfix does exactly what you need: it prevents the files from loading if it already has been loaded, thus preventing the class redeclaration. _once后缀完全满足您的需要:如果文件已经被加载,它将阻止文件的加载,从而防止了类的重新声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM