Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.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类的方法_Php_Html - Fatal编程技术网

无法调用PHP类的方法

无法调用PHP类的方法,php,html,Php,Html,我有一个简单的问题!:-D 我是全新的Php,我编写了一个简单的code,这样我就可以通过SSH连接到设备。 但是当我在一个类和方法中尝试这一点时,它不起作用。 以下是我的主要Php代码,其中也包括我的类: 试试这个: <?php class Connection { public $ip; public $usernam; public $password; public $ssh; public function sshConnection(

我有一个简单的问题!:-D 我是全新的
Php
,我编写了一个简单的
code
,这样我就可以通过
SSH
连接到设备。 但是当我在一个类和方法中尝试这一点时,它不起作用。 以下是我的主要Php代码,其中也包括我的类:

试试这个:

<?php
class Connection {
    public $ip;
    public $usernam;
    public $password;
    public  $ssh;

    public function sshConnection($ip, $username, $password) {
        include ('./view/login.html');
        include('Net/SSH2.php'); 

        $this->ip = $ip; // input type to get ip in html file
        $this->usernam = $username; // input type to get username in html file
        $this->password = $password; // input type to get password in html file
        $this->ssh = new Net_SSH2($this->ip); 
        if (!$this->ssh->login($this->usernam, $this->password)) {
            print('Login faild');
        } 
        else {
            echo $this->ssh->exec('pwd');
        }
    }
}

if(isset($_POST['login'])) {
    $connection=new Connection();
    $connection->sshConnection($_POST['ip'], $_POST['username'], $_POST['password']);
}

?>

所以,正如我在评论中所说的,您的代码应该是这样的(我添加了一点额外功能-isset()可以在一次调用中检查多个变量):


感谢我的朋友@arbogastes,我的问题非常简单!
我忘记为我定义的所有变量添加$this。
还要感谢“回声”和“天光”。
所以我的代码是这样的:

<?php
             class Connection {
             public $ip;
             public $usernam;
             public $password;
             public  $ssh;

            public  function sshConnection() {

                include ('./view/login.html');
                include('Net/SSH2.php'); // I use phpseclib to connect via SSH
                if(isset($_POST['login'])) { // Login button in html file



             $this->ip = $_POST['ip'];
             $this->usernam = $_POST['username'];
             $this->password = $_POST['password'];
             $this->ssh = new Net_SSH2($this->ip);
            if (!$this->ssh->login($this->username, $this->password)) {
            exit('Login Failed');
            print('Login faild');
        } else {
        echo $ssh->exec('pwd');

        }
         }
           }
         }

         $connection=new Connection();

         $connection->sshConnection();
?>


为什么要使用
if(isset($\u POST['login'])){
在类/函数内部?您是否也在类外部使用它?您可以将include语句放在类/函数外部class@Echoes:不,我不在类外使用它。只在类内使用。@ZenWright:当我在类外放置include时,以及在代码中有许多未定义的变量之后。为什么要使用$ssh when您上面的一行将值分配给$this->ssh而不是$ssh。对于$username,$password也是如此。您在public$usernam中也有输入错误;
<?php
             class Connection {
             public $ip;
             public $usernam;
             public $password;
             public  $ssh;

            public  function sshConnection() {

                include ('./view/login.html');
                include('Net/SSH2.php'); // I use phpseclib to connect via SSH
                if(isset($_POST['login'])) { // Login button in html file



             $this->ip = $_POST['ip'];
             $this->usernam = $_POST['username'];
             $this->password = $_POST['password'];
             $this->ssh = new Net_SSH2($this->ip);
            if (!$this->ssh->login($this->username, $this->password)) {
            exit('Login Failed');
            print('Login faild');
        } else {
        echo $ssh->exec('pwd');

        }
         }
           }
         }

         $connection=new Connection();

         $connection->sshConnection();
?>