To use with a database connection, we will keep the database access credentials as their own class constants. Also, we will use PDO for all sorts of database operations. To create the Base
class, go through the following steps:
1. Create a new PHP file inside the
Dao
directory namedBaseDao.php
, and type in the following class:<?php namespace My\Dao; abstract class BaseDao { private $db = null; const DB_SERVER = "localhost"; const DB_USER = "root"; const DB_PASSWORD = "root"; const DB_NAME = "user"; } ?>
You can see that this class
uses namespace My\Dao;
, and there is also anabstract
keyword before the class name, which defines the class as abstract. This means that the class cannot be instantiated or has at least a single abstract method inside. Also, you can see the added class constants, which contain the database information and a private class variable$db
to hold the database connection. You may modify those constants as per your requirements...