PHP 5.5 静态函数语法问题



运行PHP 5.5.38,创建一个基本的静态函数来检查文件是否存在。

我已经尝试了很多不同的变体,但看不出我在这里出了问题。

<?php
class Configuration {
    static function getDetails() {
            private $fileContents;
        if(file_exists(".configurationconfig.conf")) {
           $this->fileContents = file_get_contents(".configurationconfig.conf");
        }
        elseif(file_exists("..configurationconfig.conf")) {
            $this->fileContents = file_get_contents("..configurationconfig.conf");
        }
        else { $this->fileContents = "Config File Not Found"; }
        // Clear cache
        clearstatcache();
        if(!$config = str_replace(" ", "", $fileContents)) {
            echo "No configuration file";
            die();
            return false;
        }
        foreach(explode("n", $config) as $value) {
            $value = trim($value);
            if(substr($value, 0, 2) == '//' || substr($value, 0, 2) == '/*' ||
                    substr($value, 0, 2) == '#' || $value == "n" || $value == ""){
                continue;
            }
            list($k, $v) = explode("=", $value);
            $configTemp[$k] = $v;
        }
        return (object)$configTemp;
    }
}
?>

我得到的错误输出,

Parse error: syntax error, unexpected 'private' (T_PRIVATE) in line 8

方法中不能有private $fileContents;

类属性$fileContents必须在函数外部,因为它属于类。此外,您不应该在静态变量中调用私有变量 OR 函数。

最新更新