I'm starting with object oriented programming, but I know PHP procedural style "pretty well" I'd say. I'm working on a ticket support site for this website.
This mysqliSingleton
class was in a question I previously visited. I only added the set_charset()
method 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 which also has other methods delete
, getFromUser
, getAll
, and some others:
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
}
}
}
Am I using the singleton pattern right? But (unfortunately) 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
.
1 Answer 1
Well, although this code belongs to Codereview@stackexchenge, but as nobody cares to follow any rules here - so I would.
There are too much problems with your code, ut to outline most:
- It seems you don't quite understand what singleton is and why use it.
- There is no point in extending application class from database handler class.
trigger_error('Unknown Method ')
is apparently redundant. PHP can handle absent methods as well.- Never use die() in production code.
- This whole class makes very little sense as it don't help you to handle mysqli even a bit.
Frankly, exactly the same result you can have without any "singleton" class but just by creating mysqli instance and making it
public function __construct() {
global $mysqli;
$this->mysqli = $mysqli;
}
-
\$\begingroup\$ You have access to moderator tools, can't you just move it to codereview? \$\endgroup\$ILikeTacos– ILikeTacos2013年12月13日 16:22:00 +00:00Commented Dec 13, 2013 at 16:22
-
\$\begingroup\$ 1. You are right, as I've said im new to OOP stuff. 2. You're completly right, I haven't realized that!! 3. removed right now. 4. im not in producition mode. but thanks. 5. i didn't really understood this point! \$\endgroup\$LCH– LCH2013年12月13日 16:24:28 +00:00Commented Dec 13, 2013 at 16:24
-
1\$\begingroup\$ @AlanChavez I will start using moderator tools not sooner than at least 50% of local population will start follow site rules (namely: closing duplicated and offtopic questions). Means never. \$\endgroup\$Your Common Sense– Your Common Sense2013年12月13日 16:27:05 +00:00Commented Dec 13, 2013 at 16:27
-
\$\begingroup\$ @LCH 1. Don't use it then. 2. Just added a perfect link. 4. Any code is production. Are you going to go through ALL the code and change dirty stuff like that then moving to production? Why not to make it ALREADY the right way? 5. See example of class then helps: github.com/colshrapnel/safemysql \$\endgroup\$Your Common Sense– Your Common Sense2013年12月13日 16:30:15 +00:00Commented Dec 13, 2013 at 16:30
-
\$\begingroup\$ Your Common Sens, i'm sorry but I didnt' really understoo your edit: if I do that, wouldn't i be creating a new mysqli instance everytime i create a new object? \$\endgroup\$LCH– LCH2013年12月13日 16:30:31 +00:00Commented Dec 13, 2013 at 16:30
Explore related questions
See similar questions with these tags.
htmlentities
on a string before you store it in a database? That function (or rather:htmlspecialchars
) is for outputting text in an HTML context. \$\endgroup\$mysqliSingleton
every time you need a database call? \$\endgroup\$$this->mysqli->whatEverMethod()
. How should I do that? \$\endgroup\$