I wanted to start my a new project but this time using mysqli object, here is what I have for the database class:
<?php
declare(strict_types = 1);
namespace A\B\App;
class Database
{
private $server = "abc";
private $user = "abc";
private $pass = "abcabc";
private $name = "localhost";
public function __construct()
{
$this->connect();
}
public function connect()
{
$mysqli = new mysqli($server, $user, $pass, $name);
if($mysqli->connect_errno)
{
echo "failed to connect";
exit();
}
}
}
and here on my index page im simply calling the database class just to see if the connection is all good:
<?php
use A\B\App\Database;
require_once("../../vendor/autoload.php");
$db = new Database();
but this throws a fatal error:
Fatal error: Uncaught Error: Class "A\B\App\mysqli" not found in /Applications/MAMP/htdocs/ab/src/App/Database.php:24 Stack trace: #0 /Applications/MAMP/htdocs/ab/src/App/Database.php(18): A\B\App\Database->connect() #1 /Applications/MAMP/htdocs/ab/src/Pages/index.php(10): A\B\App\Database->__construct() #2 {main} thrown in /Applications/MAMP/htdocs/ab/src/App/Database.php on line 24
Please note that the composer autoload is working correctly since I tried to simply echo text from the __construct
method in the database and it showed perfectly fine.