Search code examples
phpphp-parse-error

why am I getting an "unexpected t_public" error?


public function getUserRoles()
{
    public $query = "SELECT * FROM user_roles WHERE userID = ".floatval($this->userID)."ORDER BY addDate ASC";
    if ($query_run = mysql_query($query))
    {
        public $resp = array();
        while ($query_row = mysql_fetch_array($query_run))
        {
            $roleID = $query_row['roleID'];
        }
        return $resp;
    }
}

I am getting the error : Parse error: syntax error, unexpected T_PUBLIC in /Applications/XAMPP/xamppfiles/htdocs/acltut/assets/php/class.acl.php on line 34.Line 34 in this case would be the 3rd line where it says "public $query".

Shouldn't variables be given "visibility" or "permissions" like var/public/private/protected/etc.?

and if that's the case, wouldn't the next line have to be written as:

if (public $query_run = mysql_query($this->query)) {}

I'm confused on when you have to include public/private/protected and refer to the variable with $this-> and when you can just create a variable.


Solution

  • Public, protected, and private provide scope resolution for class functions (methods) and member variables. You would have to do something like:

    class User
    {
        public $query;
    
        public function getUserRoles()
        {
            $this->query = "SELECT * FROM user_roles WHERE userID = ".floatval($this->userID)."ORDER BY addDate ASC";
        }
     }