unexpected T_VARIABLE, expecting T_FUNCTION

前端 未结 5 1070

I am expecting this to be a basic syntax error I overlooked, but I can\'t figure it out.

In a PHP script, I keep getting the following error.

Parse e         


        
相关标签:
5条回答
  • 2020-12-15 18:26

    Use access modifier before the member definition:

        private $connection;
    

    As you cannot use function call in member definition in PHP, do it in constructor:

     public function __construct() {
          $this->connection = sqlite_open("[path]/data/users.sqlite", 0666);
     }
    
    0 讨论(0)
  • 2020-12-15 18:35

    You cannot use function calls in a class construction, you should initialize that value in the constructor function.

    From the PHP Manual on class properties:

    This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

    A working code sample:

    <?php
        class UserDatabaseConnection
        {
            public $connection;
            public function __construct()
            {
                $this->connection = sqlite_open("[path]/data/users.sqlite", 0666);
            }
            public function lookupUser($username)
            {
                // rest of my code...
                // example usage (procedural way):
                $query = sqlite_exec($this->connection, "SELECT ...", $error);
                // object oriented way:
                $query = $this->connection->queryExec("SELECT ...", $error);
            }
        }
    
        $udb = new UserDatabaseConnection;
    ?>
    

    Depending on your needs, protected or private might be a better choice for $connection. That protects you from accidentally closing or messing with the connection.

    0 讨论(0)
  • 2020-12-15 18:35

    put public, protected or private before the $connection.

    0 讨论(0)
  • 2020-12-15 18:47

    You can not put

    $connection = sqlite_open("[path]/data/users.sqlite", 0666);

    outside the class construction. You have to put that line inside a function or the constructor but you can not place it where you have now.

    0 讨论(0)
  • 2020-12-15 18:48

    check that you entered a variable as argument with the '$' symbol

    0 讨论(0)
提交回复
热议问题