Troubleshooting unexpected T_PUBLIC error
php
Solution
<?PHP
class Session{
private static $instance;
function __construct()
{
session_start();
echo 'Session object created<BR><BR>';
}
public static function getInstance()
{
if (!self::$instance) {
self::$instance = new Session();
}
return self::$instance;
}
}
Try that. You had an extra bracket.
The error was actually in the line `function __construct()`. It created a function and then an empty set of brackets (doesn't actually error).
Then, you never ended up breaking out of the construct function so it error-ed when you tried to use the public parameter inside a function, which is not valid syntax.
This is why we make consistent bracket placement, so we always put stuff in the same place, and thus can easily spot mis-placements.
Problem
I get this error... Parse error:syntax error, unexpected T_PUBLIC in C:\filename here on line 12 On this line.... ``` public static function getInstance(){ ``` The code... ``` <?PHP class Session{ private static $instance; function __construct() { { session_start(); echo 'Session object created<BR><BR>'; } public static function getInstance(){ if (!self::$instance) { self::$instance = new Session(); } return self::$instance; } } ```