Class my_connect{ private $conn; Private function Get_conn () { $conn =new mysqli (' localhost ', "root", "", "Test", 3306); $conn->set_charset ("UTF8"); return $conn; } Private function __destruct () { if (isset ($this->conn)) { $this->conn->close (); } } static function Get_instance () { $new _conn=new my_connect (); $conn = $new _conn->get_conn (); return $conn; } } When I do $conn =my_connect::get_instance (); To the destructor $conn variable is null, which is not executed $this->conn->close ();
Reply to discussion (solution)
When setting, do you want to use $this->?
Obviously this step is wrong, this step to determine whether the variable has been defined, private $conn; Here you have defined, although nothing
if (Isset ($this->conn)) {
Just change it.
if ($this->conn) {
I went to see the wrong ....
Get_conn is a private method,
Private function Get_conn
Switch
Public function Get_conn
Can
This class you can optimize optimization, still need to optimize
Mysqli, why not just use mysql_connect?
Watch the crowd.