0

I'm declaring a simple database class that includes an array of prepared statement, but for the life of me, I can't find the syntax error here.

class Database {
    private static $users_table = "users";
    private static $statements = array("username_available" => "SELECT COUNT(*) FROM " . self::$users_table . " WHERE Username='?'");
}

Any help here?

4 Answers 4

1

You should not have quotes around the ? for your parameter. Also, you cannot declare the private static $statements as an array. Instead, you must initialize it in the constructor.

class Database {
    private static $users_table = "users";
    private static $statements;

    public function __construct() {
      $this->statements = array("username_available" => "SELECT COUNT(*) FROM " . self::$users_table . " WHERE Username='?'");
      // etc...
    }

}
Sign up to request clarification or add additional context in comments.

3 Comments

I'm using this class as a static class, so there won't be any instantiation. Should I just declare it in the function where it's used?
@pythonscript Yes, just create it in the function that will use it.
@pythonscript - Or just change $statements into a method that returns the array (see my answer), and use it w/ the command self::statements()
0

you cannot concatenate while assigning values on class variables declaration: you can assign just a scalar value or another array (or an object if I remember correctly).

You can assign expressions into a method, like the class constructor, or maybe another static method

class Database {
    private static $users_table = "users";
    private static $statements = null;
    public function __construct(){
        if(self::$statements === null){
            self::$statements = array("username_available" => "SELECT COUNT(*) FROM " . self::$users_table . " WHERE Username=?");
        }
    }
}

This code should work in your case. And as Michael said you should remove quotes around the question mark.

1 Comment

Thanks for the help; I think I'll be using isset, but the point is the same. Thanks again!
0

I don't think PHP likes declaring static class variables from concatenations or otherwise requires evaluation. You can't set a static variable to the result of a function call either:

private static $time = time(); # this will fail too. Value must be a literal value

In other words, a propety values in a class definition can't come from something that's evaluated.

So you have to hardcode the value of the $users_table string into the $statements:

private static $statements = array("username_available" => "SELECT COUNT(*) FROM `users` WHERE Username=?");

Comments

0

You're having problems due to requiring an evaluation when setting a class property.

This is because a PHP class property, "must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.". See the examples on the previously linked page.

To keep the same format you could just have a static method, that simply returns the value you want:

<?php
class Database {
    private static $users_table = "users";
    private static function statements() {
           return array("username_available" => 
                 "SELECT COUNT(*) FROM " . self::$users_table . " WHERE Username=?");
    }
}
?>

Working example

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.