Using a database class in my user class

前端 未结 6 609
慢半拍i
慢半拍i 2021-02-09 01:00

In my project I have a database class that I use to handle all the MySQL stuff. It connects to a database, runs queries, catches errors and closes the connection.

Now I

6条回答
  •  迷失自我
    2021-02-09 01:29

    Simple, 3 step process. 1/ Create a database object. 2/ Give it to your user class constructor. 3/ Use it in the user methods.

    Little example.

    File Database.class.php :

    In User.class.php :

    _db = $db;
      }
    
      public function deleteUser(){
        $this->_db->query('DELETE FROM Users WHERE name = "Bobby"');
      }
    }
    

    Now, in userManager.php for example :

    deleteUser();
    

    If you want the current trendy name of this old technique, google "Dependency Injection". The Singleton pattern in php will fade away soon.

提交回复
热议问题