I picked up this class that connects to MySql via PDO, in that link . It works correctly. But I decided to turn it into a static class. And here is the modified code.
class db {
private static $dsn;
private static $username;
private static $password;
private static $db;
private static $conn;
private static $arrConnAttr;//Array of connection attributes. Ex: ["PDO::ATTR_ERRMODE", "PDO::ERRMODE_EXCEPTION"]
private static $pathErroLog;//Path to save the erro log files. Ex:$_SERVER['DOCUMENT_ROOT'] . "/myDirRootProject/LogError"
private static $arrCatchConnResult;//Path to save the erro log files. Ex:$_SERVER['DOCUMENT_ROOT'] . "/myDirRootProject/LogError"
private static $die;//If we set $setValues["die"]=NULL, during the object initialization, errors will not stop the process.
public static $sql;
public function __construct(array $setValues=NULL) {
xdebug_break();
$arrInitDefault["dsn"]="localhost";
$arrInitDefault["username"]= "root";
$arrInitDefault["password"]="";
$arrInitDefault["db"]="drset2014";
$arrInitDefault["arrConnAttr"]= ["PDO::ATTR_ERRMODE"," PDO::ERRMODE_EXCEPTION"];
$arrInitDefault["pathErroLog"]= dirname(__FILE__)."/Log/Error";//Default path to dir. Note: error pathLog can be "/Log/Error/UserName" or "/Log/Error/UserId", etc
$arrInitDefault["die"]= TRUE;
$arrInitDefault["sql"]= FALSE;
if(!is_null($setValues)){
$arrInitDefault= array_merge($arrInitDefault,$setValues);
}
//After merge, initialaze private variables with result merged $arrValues
self::$dsn = $arrInitDefault["dsn"];
self::$username= $arrInitDefault["username"];
self::$password= $arrInitDefault["password"];
self::$db= $arrInitDefault["db"];
self::$arrConnAttr= implode(",", $arrInitDefault["arrConnAttr"]);
self::$pathErroLog=$arrInitDefault["pathErroLog"];
self::$die=$arrInitDefault["die"];
//This try and catch is only to estabilish the connection during the initialization
try {
$dns=self::$dsn;
$username=self::$username;
$password=self::$password;
$db=self::$username;
self::$conn= new PDO("mysql:host=$dns; dbname=$db", "$username", "$password");//Now private $conn is a PDO object
self::$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}// End of try
catch(PDOException $e){//If had some error. The PDO object ($this->conn) could not be created. Throw an error.
self::$arrCatchConnResult = self::saveLogError($e->getMessage());//Save a message in the log file and throw a message
$msg=self::$arrCatchConnResult["strToHTML"];
self::$conn=null;
if(self::$die){
die($msg);
}
}
}
public static function saveLogError($msg=NULL,$sql=NULL) {
#xdebug_break();
xdebug_break();
$year = date("Y");
$month = date("M");
$day = date("d");
$dt = date("d.M.Y_H.i.s");
$pathErroLog= self::$pathErroLog."/".$year."/".$month."/".$day."/";
if (!is_dir($pathErroLog)) {
mkdir($pathErroLog,0777,true);
}//If is not a dir, create it.
$sqlErrorStringTXT="";
$sqlErrorString="";
if(!is_null($sql)){
$sqlErrorStringTXT = "Sql: $sql";
$sqlErrorStringHTML = "<kbd>Sql</kbd>: $sql";
}
/////////////// Maybe should be good save errors in database too.
$strToTxt=<<<EOF
>>>>> Mensagem de erro: <<<<< \n
Date: $dt \n
Msg: $msg \n
$sqlErrorStringTXT \n
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
EOF;
//Save error message to the file
$f=$pathErroLog."$dt.txt";
$myfile = fopen($f, "w") or die("Unable to open file!");
fwrite($myfile, $strToTxt);
fclose($myfile);
//Create a string to be displayed in the broser
$strToHTML=<<<EOF
<div class="alert alert-danger" role="alert">
Date: $dt <br/>
Msg: <span class="label label-danger">$msg</span><br/>
$sqlErrorStringHTML<br/>
</div>
EOF;
$arrCatchConnResult["status"]=0;
$arrCatchConnResult["strToTxt"]=$strToTxt;
$arrCatchConnResult["strToHTML"]=$strToHTML;
return $arrCatchConnResult;
}//End of saveLogError()
public static function get_values() {
try {
$stmt = self::$conn->prepare(self::$sql);
$stmt->execute();
while ($field = $stmt->fetch(PDO::FETCH_ASSOC)) {
$arrResult[]=$field;
}
return $arrResult;
}
catch (PDOException $e) {
self::$arrCatchConnResult = self::saveLogError($e->getMessage(), self::$sql);//Save a message in the log file and set a message
$msg= self::$arrCatchConnResult["strToHTML"];
self::$conn = null;
if(self::$die){
die($msg);
}
}
}
}//End of class db
On my index.php page, I'm calling it this way:
include './class/db.php';
db::$sql ="select * from city";
echo "<pre>";
print_r(db::get_values());
echo "</pre>";
When I run the page I get an error of type:
Fatal error: Call to a member function prepare() on null in C:\xampp\htdocs\drReference\private\class\db.php on line 126
It seems that when I transform in static class the constructor is not called, since in these types of classes the creation of objects is not necessary. I could try to solve this by passing the code from the con- troller to a connect function for example, and call this function every time it was to start a query.
My question is, is there any way to continue with the static class and force boot through the constructor?