How to organize MVC architecture in PHP? I tried to do this, and it works, but in my code model connects to database each time it is loaded. Is that possible to use previous connection?
Here are my files (not making any queries in model, for example purposes):
index.php
include 'controller/home.php';
$home = new HomeController();
$home->index();
controller/home.php
class HomeController {
public function index () {
require('model/home.php');
require('model/another.php');
$home = new HomeModel();
echo $home->foo();
echo $home->bar();
$another = new AnotherModel();
echo $another->baz();
}
}
model/home.php
class HomeModel extends mysqli {
public function __construct() {
parent::__construct('localhost', 'root', '', 'mydb');
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
}
public function foo () {
return true;
}
public function bar () {
return false;
}
}
model/another.php
class AnotherModel extends mysqli {
public function __construct() {
parent::__construct('localhost', 'root', '', 'mydb');
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
}
public function baz () {
return true;
}
}