I have a function that gets a value from the database and returns it. I call the function to store it into a member variable but I get the following error:
Parse error: parse error, expecting `','' or `';'' in I:\wamp\www\deposit\classes\Site.php on line 14
This is the line that causes the error
public static $depositmoney = self::get_balance();
And this is the function that gets the value from the database
public static function get_balance()
{
global $link, $usertable, $userid, $useridentify;
//query current balance
$cb = mysqli_fetch_object(mysqli_query($link, "SELECT deposit FROM ".$usertable." WHERE ".$userid."=".$useridentify.""));
return $cb->deposit;
}//end of function get_balance().
all of this code is in the same class. Anyone an idea of what’s causing the error?
Class properties may not be declared with run-time information.
The above will not work.
See the PHP Manual on Class Properties: (emphasis mine)
You could create a getter for
$depositmoneyand have it initialize the value if it is currently unset:However, I suggest to get rid of the
staticand use instance methods and properties instead to track the state. You will also want to get rid of theglobalstuff and inject dependencies through the constructor, setters or during the method invocation. That decreases coupling and will make the code more maintainable.