this is my first post on the whole StackExchange network, so I might make some mistakes. And I'm also spanish spearker, so I'll probably have mistakes in my writting too..
Context: I'm starting with object oriented programming, but I know php prodecudral style "pretty well" i'd say. So, I'm working on a ticket support site for this website. This is how it goes:
This mysqliSingleton Class was in a question I previusly visitted. I only added the set_charset()
methedo to feet my database. By the way, should that line be in the __constructor
or in the init
method?
class mysqliSingleton {
private static $instance;
private $connection;
private function __construct() {
$this->connection = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
$this->connection->set_charset('utf8');
}
public static function init() {
if(is_null(self::$instance)) {
self::$instance = new mysqliSingleton();
}
return self::$instance;
}
public function __call($name, $args) {
if(method_exists($this->connection, $name)) {
return call_user_func_array(array($this->connection, $name), $args);
} else {
trigger_error('Unknown Method ' . $name . '()', E_USER_WARNING);
return false;
}
}
}
This is a piece of my ticket class wich also has other methos delete, getFromUser, getAll, and some other:
class Ticket extends mysqliSingleton {
private $mysqli;
public function __construct() {
$this->mysqli = mysqliSingleton::init();//Singleton db
}
//Submit a ticket
public function submitTicket($idusuario, $problem,$moreinfo){
$query = "INSERT INTO tbl_name (tbl_field,tbl_problem,tbl_moreinfo) VALUES (?,?,?)";
$stmt = $this->mysqli->prepare($query) or die($this->mysqli->error);
if ($stmt) {
$stmt->bind_param('iss',$idusuario,$problem,htmlentities($moreinfo));
if ($stmt->execute()) {
return true;
} else return false;//Problem sql
}
}
}
So my main question would be to know if I'm using singleton pattern right.
But (unfortunatly) after I code that, I starting reading more about abstract classes and Interfaces, and I've started to doubt if this is right. By the way, the ticket class, is one of many classes that will extend to mysqliSingleton. I'm sorry if it's too long but im kinda new to this question&andswer stuff :P
Thank you very much.
htmlentities
on a string before you store it in a database? That function (or rather:htmlspecialchars
) is for outputting text in an HTML context. – Marcel Korpel Dec 13 '13 at 16:07mysqliSingleton
every time you need a database call? – Your Common Sense Dec 13 '13 at 16:10$this->mysqli->whatEverMethod()
. How should I do that? – LCH Dec 13 '13 at 16:11