Class variable is null when I try to access it in PHP

隐身守侯 提交于 2021-02-17 06:21:09

问题


Alright, this is my main code:

require "checkpassword.php";
require "mysqllogininfo.php";

# Validate password
if (!validatePassword($_GET["password"])) {
    return;
}

# Get variables
$uuid = $_GET["uuid"];
if (preg_match('/^\d+$/',$_GET["rank"]) == false) {
    die("Rank must be integer");
}
$rank = $_GET["rank"];

# Validate UUID
if ($uuid == null) {
    die ("Supply uuid");
}

# Validate rank
if ($rank == null) {
    die ("Supply rank");
} else if ($rank < 0 || $rank > 6) {
    die ("Invalid rank");
}

# Load MySQL login info
$loginInfo = new MySQLLoginInfo("/var/uniqraft/mysqllogin");

# Debug
# All this displays as just 3 empty lines, so I assume the variables are null <--
echo($loginInfo->host);
echo "<br>";
echo($loginInfo->username);
echo "<br>";
echo($loginInfo->password);
echo "<br>";
echo($loginInfo->dbname);

# Create connection
$connection = mysqli_connect($loginInfo->host, $loginInfo->username, $loginInfo->password, $loginInfo->dbname);

# Check connection
if (mysqli_connect_errno()) {
    die ("Failed to connect to MySQL: " . mysqli_connect_error());
}

As you can see, I'm trying to access the variables host, username, password and dbname in the MySQLLoginInfo object, but they all display as null.

However, I don't think they are actually null though. Because, if we take a look at the MySQLLoginInfo object, we can see I added some debug echo()'s to display the variables, and there we see that they are in fact not null.

class MySQLLoginInfo
{
    # Properties
    public $host;
    public $username;
    public $password;
    public $dbname;

    # Constructor
    function __construct($infoLocation) {
        $handle = fopen($infoLocation, "r", false) or die("Couldn't open $infoLocation");

        $i = 0;
        # Loop through lines
        while (($line = fgets($handle)) !== false) {
            if ($i === 0)
                $host = $line;
            else if ($i === 1)
                $username = $line;
            else if ($i === 2)
                $password = $line;
            else if ($i === 3)
                $dbname = $line;
            else
                break; # Read at max 4 lines
            $i++;
        }
        if ($i < 3) {
            die($infoLocation . " doesn't contain at least 4 lines");
        }

        # These all display the proper values, indicating that they are not null <--
        echo $host;
        echo $username;
        echo $password;
        echo $dbname;
    }
}

The final output is as follows:

localhost mcserver secret_password uniqraft_core

Failed to connect to MySQL: Access denied for user 'www-data'@'localhost' (using password: NO)

Note that there are empty lines between the 2 texts, from the output from the echo()'s in the main code.

So, my questions is, why are the variables stored with proper values in the MySQLLoginInfo object, but when I try to access them they are null?

Any help would be greatly appreciated.


回答1:


No where in your MySQLLoginInfo constructor do you actually set the class variables, only the local function variables:

#Loop through lines
while (($line = fgets($handle)) !== false) {
    if ($i === 0)
        $this->host = $line; // vs $host = $line;
    // ...
}

I refer you to the documentation on Class Properties:

Within class methods non-static properties may be accessed by using -> (Object Operator): $this->property (where property is the name of the property). Static properties are accessed by using the :: (Double Colon): self::$property. See Static Keyword for more information on the difference between static and non-static properties.



来源:https://stackoverflow.com/questions/25084859/class-variable-is-null-when-i-try-to-access-it-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!