In PHP, when creating a class, there is a way to call a certain method every time that class is initialized. This is called the constructor of the class. Most classes have a constructor, and so we shall have our own. The constructor function is named with two underscores with the construct()
keyword, like this: function __construct()
. Functions with two underscores are also known as magic methods.
In our database abstraction class we need to create a constructor to be able to return the link
object generated by mysqli
:
Class DB { public $db; //constructor function __construct($server, $dbname,$user,$pass) { //returns mysqli $link $link = mysqli_connect(''); return $this->db = mysqli_connect($server, $dbname, $user, $pass); } }