Building a User Login System in PHP with OOP pt.II

KolaKachi
This entry is part 23 of 24 in the series PHP Object-Oriented Programming(OOP) Crash Course

Welcome back to our PHP Object-Oriented Programming (OOP) series. In the previous article, we began constructing a powerful database connection class, and now we’re ready to delve into the next steps. In this part, we’ll focus on creating instances of tables, setting up a database connection, and ensuring that our code is secure and efficient.

Creating Table Instances:

In the previous article, we set up the structure for our Database class, including functions such as select and update. Now, let’s look at the table function and how we can create instances of tables.

class Database
{
    // ... (previous code)

    protected static $table;

    // Set the table name for the query.
    public function table($table) {
        self::$table = $table;
        return $this; // Allow method chaining.
    }
}

In the table function, we accept a table name as a parameter and set the static $table variable to that value. This allows us to create instances of tables and access them in subsequent functions.

Initializing the Connection:

Now, let’s initialize the database connection. We use PDO (PHP Data Objects) for secure database interactions.

class Database
{
    // ... (previous code) 

    // Create a database connection.
    public function __construct() {
        try {
            $string = "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME;
            self::$connection = new PDO($string, DB_USER, DB_PASS);
        } catch (PDOException $e) {
            die("Connection failed: " . $e->getMessage());
        }
    }
    
    // Singleton design pattern to create a single instance.
    public static function getInstance() {
        if (!self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    // ... (more code)
}

Here, we’ve implemented the Singleton design pattern to ensure a single instance of our Database class. The __construct function establishes a PDO connection using the database details stored in the initialization file.

Handling Table Instances:

Let’s go back to the table function and understand how it fits into the bigger picture.

class Database
{
    // ... (previous code)

    // Set the table name for the query.
    public static function table($table) {
        self::$table = $table;
        return self::$instance; // Allow method chaining.
    }
    
    // ... (more code)
}

In this function, we set the static $table variable to the specified table name, and crucially, we return $this. Returning $this enables method chaining, a powerful technique where multiple methods can be called in a single line.

Query Methods:

class Database
{
    // ... (previous code)
    
    public function select() {
        // Function for reading data from the database (to be implemented).
    }
    
    public function update() {
        // Function for updating data in the database (to be implemented).
    }

    protected function run() {
        // Internal function for executing SQL queries (to be implemented).
    }
}

SELECT Query Method:

public function select() {
    // Implementation for SELECT query.
}
  • Placeholder for the implementation of the SELECT query.
  • Additional code will be added here to handle SELECT queries.

UPDATE Query Method:

public function update() {
    // Implementation for UPDATE query.
}
  • Placeholder for the implementation of the UPDATE query.
  • Subsequent updates will fill in the logic for UPDATE queries.

Query Execution Method:

protected function run() {
    // Implementation for query execution.
}
  • Placeholder for the implementation of the query execution logic.
  • This method will handle the execution of the constructed queries.

Conclusion:

In this part of the practical project for our PHP OOP course, we have laid the foundation for a powerful database connection class. We’ve created a secure database connection using PDO, embraced the Singleton design pattern for efficiency, and provided a mechanism to set the target database table.

Stay tuned for the next part of this project, where we will dive deeper into implementing the select and update functions to read and modify data in the database.

Series Navigation<< Building a User Login System in PHP with OOP pt.IBuilding a User Login System in PHP with OOP pt.III >>

Leave a Reply

Your email address will not be published. Required fields are marked *