Code First Approach

code first approach into core PHP

This article contains examples and best practices for using a code first approach in Core PHP. It is intended for developers who are looking to understand and implement this methodology in their PHP projects

Required Files and Classes

1: PHP Configuration file:

This file contains configuration of your server and database in my case i used config.php, that is look like this:


$config = [
    "host" => "localhost",
    "username" => "root",
    "password" => "",
    "database" => "php_code_first_approach_db",

2:Connection file:

To connect to the database, you need another file for connect to database that where you have to make a function that will take your configuration into parameters in my case I make the file connection.php and call the connectToDatabase function from the connection.php file. Here’s how the connection.php file looks:

function connectToDatabase($config)
    $conn = mysqli_connect($config['host'], $config['username'], $config['password'], $config['database'])or die("lost");
    if (!$conn) {
        die('Connection failed: ' . mysqli_connect_error());

    return $conn;

3 Base Class for Migration Classes:

Now you have to create a base class of all migration file(classes) that will be inherited into all migration files(classes), To create a table in the database, you can use the static funtion  from the Base Migration class . Here’s how the Base Migration class looks:


class BaseMigration {
    public static function createTable($conn, $sql) {
        if ($conn->query($sql) === TRUE) {
            echo "Table created successfully";
        } else {
            echo "Error creating table: " . $conn->error;

4: Create Table File(Class):

The Base Migration class is extended by other classes to create specific tables. For example, the RoleMigration class creates a tbl_userRoles table if it doesn’t exist. Here’s how the RoleMigration class looks:


class RoleMigration extends BaseMigration
    public static function up($conn)
        $sql = "SHOW TABLES LIKE 'tbl_userRoles'";
        $result = $conn->query($sql);
        if ($result->num_rows == 0) {
            $sql = "CREATE TABLE tbl_userRoles (
            name VARCHAR(30) NOT NULL,
            self::createTable($conn, $sql);
            echo "tbl_userRoles has created";
            echo "tbl_userRoles already exist";

5: Table Creation:

After setting up the database connection and defining your migration classes, you need to execute the migrations to create the tables in your database. In my case this is done in the Migrate.php file, you can use any meaning full name. Here’s how the Migrate.php file looks:

require_once '../config/config.php';
require_once '../config/connection.php';
require_once 'BaseMigration.php';
require_once 'RoleMigration.php';
require_once 'UserMigration.php';

$conn = connectToDatabase($config);

$roleMigration = RoleMigration::up($conn);

Alternatively, if you have a local server environment set up (like XAMPP, WAMP, or MAMP), you can also navigate to the Migrate.php file through your web browser by typing the local server URL followed by the path to the Migrate.php file. For example:


if your table will not exist that will be created


The Code-First Approach in Core PHP is a powerful methodology that allows developers to focus on the code and logic of the application first, rather than the database design. This approach can lead to cleaner, more maintainable code, and it can also make it easier to adapt to changes in the application’s requirements.

You can also try my repository, this repository provides a solid foundation for implementing a code-first approach in your PHP projects. It includes examples of how to connect to a database, create tables, and execute migrations. While it currently only supports creating tables, the goal is to develop a full-fledged migration system for Core PHP.

Jobs Update:

Leave a Comment

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