Creating a MySQLi wrapper with the Singleton Pattern (Part 2/2)

preview_player
Показать описание

Official site

Twitter
Рекомендации по теме
Комментарии
Автор

Nice tutorial.
One queestion ive read in php design patterns (o'reilly) that in a singleton the constructor needs to be provate/protected and the __clone method too.
So you ensure that only one Instance will be created in runtime.

zugdsbtngizudsgbnudsdsoiu
Автор

If you havent solved it yet, this is how I got this. Create a private field $last_id, create an insert\update method(s) which specifically handle insert\update queries and stores the id once its query has been run i.e.

private $last_id = -1;

public function insert\update($sql) {
// execute query here...
$last_id = $mysqli->insert_id;
return $this;
}
public function lastID() { return $this->last_id; }
$id = INTO users(`Alex`, `Al3x`)')->lastID();

gonzo
Автор

What if I wanted to query different databases, I guess I would have to create another class with different connection parameters right? is it still worth using this pattern in that case?

martmelee
Автор

use $this->_results = array(); just before the while loop that writes the results to the array

TheiiSa
Автор

Do you have to reset the $_results array and the $_count variables in the query method call? If I don't and I call this $users = * FROM users'); and I call it again but with a different variable name, I get the results from the first time I called it.

HbGamers
Автор

Do you have a tutorial on what public/protected/private and static do?

ThaRSGeek
Автор

hi, im totally new in php, i want to ask about singleton, if I have 2 php file, both require_one db.php. Is it normal that number of connection in mysql increase? is it not make the server refuse connection if reach the max connection?

spinx
Автор

How do I execute a DML query using this method?

miskaamuskaa
Автор

how can i use mysql_insert_id with your script?

MrMonat
Автор

I always think that singleton pattern must allow to create only one instance of object, but your example allow to create more then one instance, because you have a public construct method and you not override magic method clone:
stackoverflow.

di
visit shbcf.ru