0

Running PHP 5.5.38, making a basic static function to check if a file exists.

I've tried a lot of different variations and can't see where I'm going wrong here.

<?php

class Configuration {

    static function getDetails() {

            private $fileContents;

        if(file_exists(".\configuration\config.conf")) {
           $this->fileContents = file_get_contents(".\configuration\config.conf");
        }
        elseif(file_exists("..\configuration\config.conf")) {
            $this->fileContents = file_get_contents("..\configuration\config.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;
    }
}

?>

Error output I'm getting,

Parse error: syntax error, unexpected 'private' (T_PRIVATE) in line 8
  • 2
    Define your properties outside of methods, not inside them – Mark Baker Nov 07 '17 at 10:58
  • Also you cannot use `$this` variable inside static methods. – Jakub Matczak Nov 07 '17 at 11:02
  • Yep, both of the above worked. But I don't really understand why those things aren't allowed. Is it a 'PHP' thing? – user2951106 Nov 07 '17 at 11:07
  • Object properties are not part of the method, they're part of the class, so they are defined in classes and not in methods... that seems pretty logical, it's an OOP thing, not a PHP thing – Mark Baker Nov 07 '17 at 11:10
  • Yes but Java and other languages you can declare variables with methods, and so have method or function properties. So lets say I have a static math class with tons of functions, they're going to be accessible to every function? – user2951106 Nov 07 '17 at 11:28
  • If you have a static math class with tons of functions, then that's bad design... the whole point of class properties is that they are available to every method defined in the class, if you want a property that is only usable within one method, then you simply create a locally scoped variable in that method, not a class property.... like you'd create a local variable in a java method – Mark Baker Nov 07 '17 at 11:32
  • How would I make a locally scoped variable? Because that's essentially what I was aiming for with "private $fileContents". Apologies if this is common knowledge in PHP but I'm a beginner and I'm more used to non web based languages like C, Java, etc. – user2951106 Nov 07 '17 at 14:57

2 Answers2

2

You cannot have private $fileContents; inside a method.

Fabio Carpinato
  • 1,121
  • 6
  • 13
1

The class property $fileContents must be outside of the function, because it belongs to the class. Moreover, you should not call a private variable OR function inside a static one.

Pauloscorps
  • 564
  • 1
  • 6
  • 16