Unexpected T_VARIABLE, Expecting T_FUNCTION: A Classic Class Definition Pitfall
You are staring at a Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION on line 4 of your database connection script. The error message is technically precise but can feel frustratingly vague if you aren't immediately looking at the definition rules for PHP classes. You have stripped the code down to its bare bones, removed external dependencies, and confirmed that include_once() triggers the failure. Yet, the logic seems sound: a class, a property, and a public method.
The culprit is hidden in plain sight within the class body itself. In your provided snippet:
class UserDatabaseConnection
{
$connection = sqlite_open("[path]/data/users.sqlite", 0666); // Line 4
public function lookupUser($username)
{
// rest of my code...
}
}
PHP requires that every class property or variable definition be declared with the public, private, or protected visibility modifier before assigning a value. When the parser encounters $connection = sqlite_open(...) on line 4, it expects either a method definition (starting with function) or another property declaration that follows the visibility rules. Because the public keyword is missing before $connection, the parser fails to recognize this as a valid property declaration. It assumes you intended to define a method there, hence the error message suggesting it was expecting T_FUNCTION.
This specific syntax error often trips up developers transitioning from dynamic languages or older PHP versions where implicit visibility might have been assumed (though never for top-level properties in class bodies). The fix is straightforward: add the visibility keyword.
class UserDatabaseConnection
{
public $connection = sqlite_open("[path]/data/users.sqlite", 0666);
public function lookupUser($username)
{
// rest of my code...
}
}
While the syntax fix resolves the immediate crash, there are significant architectural and security implications in how this class is structured that you should address immediately. You are using sqlite_open() to establish a connection inside the constructor scope (implicitly) or at the class level without initializing an instance variable properly before use. More critically, you are instantiating the class without passing a username or database path into the constructor.
In modern PHP development, particularly when interacting with databases, it is best practice to move resource initialization into the constructor (__construct) and accept configuration data as arguments. This ensures that properties are not set globally for every instance if they rely on external state, and it promotes immutability and testability.
Consider refactoring your class to accept the database path in the constructor:
class UserDatabaseConnection
{
private $connection;
public function __construct($dbPath)
{
$this->connection = sqlite_open($dbPath, 0666);
if (!$this->connection) {
throw new RuntimeException("Unable to open database: " . $dbPath);
}
}
public function lookupUser($username)
{
// Logic here
}
}
Additionally, you should be aware that SQLite support via the sqlite_* functions was deprecated in PHP 7.0 and removed entirely in PHP 7.1. If you are running a version of PHP above 7.1, this code will not only fail to parse but also fail to load at runtime due to the missing extension. You would need to migrate to PDO or the newer SQLite3 extension.
Finally, regarding file inclusion: while include_once works, it can mask errors in the included file if you are not careful with error reporting levels. In a production environment, ensure that any file included via require or include is validated for existence and permissions before execution to prevent runtime failures that might otherwise look like obscure parse errors if the file itself contains syntax issues introduced by a previous edit.
If you are building larger applications where database connections are managed centrally, consider how this pattern fits into dependency injection containers often used in frameworks like Laravel. In those contexts, passing configuration explicitly is not just syntactic preference; it is a requirement for scalable architecture.
