简体   繁体   English

无法调用静态变量

[英]Can not call Static variable

I am brand new to PHP and have a simple project, I do Java. 我是PHP新手,有一个简单的项目,我做Java。 In my simple project I have some html files and some Php files. 在我的简单项目中,我有一些html文件和一些Php文件。 I connect to a device via SSH in my Php web page, and after that I am gonna run some commands and return back SSH returned data. 我在我的Php网页中通过SSH连接到设备,之后我将运行一些命令并返回SSH返回的数据。 Any ways, So I designed a Html file for Login and use that in my php , here is my Php code : 任何方式,所以我为登录设计了一个Html文件,并在我的php ,这是我的Php代码:

          <?php                
    class Connection {
    public static $ip; // I even made static this variable to test if I can access Static variable from other 
    public $username;
    public $password;
    public static $ssh;


    public  function sshConnection() {
        include ('./view/login.html'); // The html page contain variables 
        include('Net/SSH2.php'); // I use phpseclib to connect via SSH

        if(isset($_POST['lgin'], $_POST['ip'], $_POST['username'], $_POST['password'])) { // Login button in html file
            $this->ip = $_POST['ip']; // input type to get ip in html file
            $this->username = $_POST['username']; // input type to get username in html file
            $this->password = $_POST['password']; // input type to get password in html file
            $this->ssh = new Net_SSH2($this->ip);
            if (!$this->ssh->login($this->username, $this->password)) {
                print('Login faild');
            } else {

                header("Location: http://localhost/wireless/configwireless.php"); // This redirect to next page that I should display some Commands
            }
        }
    }
}
    $connection=new Connection();
    $connection->sshConnection();       
        ?>

I need $ssh variable in next page so that I can run commands via this connection and make sessions. 我需要在下一页中使用$ ssh变量,以便我可以通过此连接运行命令并进行会话。 I googled and find out I can access the static variable via this code : 我用Google搜索并发现我可以通过以下代码访问静态变量:

classname::$variableName;

I even made my $ip variable static to test if I can access that or not, But no chance, Here is my configwireless.php Code : 我甚至让我的$ ip变量静态来测试我是否可以访问它,但没有机会,这是我的configwireless.php代码:

<?php
   echo Connection::$ip; // Does not display the input ip variable.

?>

But it does not display the $ip variable. 但它不显示$ip变量。 Where I am doing wrong? 我在哪里做错了?

For assignment static property in class, should be used self or static keyword. 对于类中的赋值静态属性,应使用selfstatic关键字。 So instead of 而不是

$this->ip = $_POST['ip'];

use this 用这个

static::$ip = $_POST['ip'];

or 要么

self::$ip = $_POST['ip'];

As my dear friend @Mohammad notice, after header("Location: http://localhost/wireless/configwireless.php"); 正如我亲爱的朋友@Mohammad所说,在header("Location: http://localhost/wireless/configwireless.php");之后header("Location: http://localhost/wireless/configwireless.php"); variables will be lost. 变量将丢失。 I did this before header("Location: http://localhost/wireless/configwireless.php"); 我在header("Location: http://localhost/wireless/configwireless.php");之前做了这个header("Location: http://localhost/wireless/configwireless.php"); :

            session_start();
            $_SESSION['ip'] = $this->ip; 

And in next page I added : 在下一页中我添加了:

    session_start();
    $x=$_SESSION['ip'];
    echo $x;

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

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