0

私は楽しみのために取り組んでいるプロジェクトへのオブジェクト指向アプローチを作成しようとしていますが、データベース クラスのアイデアを理解するのに苦労しています。次のエラーが表示されます。

    Call to undefined method Database::prepare()

データベース クラス

class Database
{

    protected $connection;

    function __construct()
    {
        $this->createConnection();
    }

    private function createConnection()
    {

        $this->connection = new mysqli("localhost", "user", "password", "test");
        if ($this->connection->connect_errno)
        {
            echo "Failed to connect to MySQL: (" . $this->connection->connect_errno . ") " . $this->connection->connect_error;
        }
        else
        {
            echo 'Connected to database.<br />';
        }

    }
}

$db = new Database();

UserActions クラス

class userActions
{

    protected $_db;
    protected $_username;
    protected $_password;
    protected $_auth;
    protected $tableName;
    function __construct($db, $username, $password, $auth)
    {
        $this->_db = $db;
        $this->_username = $username;
        $this->_password = $password;
        $this->_auth = $auth;

        $this->checkUserExists();
    }

    private function checkUserExists()
    {
        $query= "SELECT COUNT(*) FROM '{$this->tableName}' WHERE username = ?";
        $stmt = $this->_db->prepare($query);
        $stmt->bind_param('s', $this->username);
        $userNumber= $stmt->execute();
        echo $userNumber;
    }
}

このタスクのやり方を改善するために何かできることはありますか?

4

1 に答える 1