Ok so I just started taking a 3rd year module on advanced web design focusing on OOP, something which I am having a hard to master, please note after working with PHP for a number of years in procedural style the switch is not easy so please consider this is a beginner question.
So my textbook has the following example to create a class Database
using PDO.
Now I know I am probably going to come under attack from the PDO
gang by saying the following but I prefer Mysqli
for the time being as I don't have enough time to get comfortable with PDO
at the moment. Thus I amtrying to convert the following class to Mysqli
but by the looks of it failing horrible in the process.
I came up with the following code:
class Database{
private static $server = 'localhost';
private static $uname = 'root';
private static $pword = '';
private static $dataBase = 'shop';
private function __construct(){
}
public static function getDB(){
if(!isset(self::$dataBase)){
self::$dataBase = new mysqli_connect(
self::$server,
self::$uname,
self::$pword
);
$db_found = mysqli_select_db(self::$dataBase);
}//if
if($db_found){
echo 'SUCCESS';
}//db found
return self::$dataBase;
}
}
Now unfortunately I don't have specific debugging errors to provide, except that I cant connect to my DB, since my console is not giving any errors, however I will point out a few areas which I believe is possibly incorrect.
- This is possibly incorrect
self::$dataBase = new mysqli_connect
- Im not sure how to select my db inside the class thus,
$db_found = mysqli_select_db(self::$dataBase);
is possibly invalid...?
ANY help / advice very much appreciated!