Why this variable is null

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.