unexpected T_VARIABLE, expecting T_FUNCTION

Ryan Leonard picture Ryan Leonard · Jun 26, 2011 · Viewed 126.4k times · Source

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 error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in [path]/scripts/users/database_connection.php on line 4

This occurs when my script to connect to the database is called with an include_once(). I stripped my script down to the most basic code (leaving in what is required by other code), and it still is calling this error.

<?php
    class UserDatabaseConnection
    {
        $connection = sqlite_open("[path]/data/users.sqlite", 0666);
        public function lookupUser($username)
        {
            // rest of my code...
        }
    }

    $udb = new UserDatabaseConnection;
?>

I have struggled with this for a while, and just wondered if anyone else could spot somewhere I went wrong.

Answer

Sabeen Malik picture Sabeen Malik · Jun 26, 2011

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.