Jump to content

Question about using mysqlli


Go to solution Solved by kicken,

Recommended Posts

Hey guys. I have been reading a tutorial on how mysqlli works... and I am little confused on how the following works.

 

Below is the database class...

<?php

Class Database {

    private $_connection;
    //Store a single instance
   private static $_instance;
   
   // Get an instance of the Database
   //@return Database
   public static function getInstance() {
   if(!self::$_instance) {
       self::$_instance = new self; 
   }
   return self::$_instance;
   }
   /**
    * Constructor
    */
   public function __construct() {
      $this->_connection = new mysqli('localhost', 'sandbox', 'sandbox', 'sandbox') ;
  if (mysqli_connect_error()) {
      trigger_error('Failed to connect to Mysqli: '.mysqli_connect_error(), 'E_USER_ERROR');
  }
      }
      
     /*
      * Empty cloning magic method to prevent duplication .
      */
  private function __clone() {}
  
   /*
      * Get mysqli connection
      */
  public function getConnection() {
  return $this->_connection;}
      
}


?>

and this is the address class that is using the database class

$db = Database::getInstance();
          $mysqlli = $db->getConnection;
      }
      

 I understand that the Database::getInstance(); gets this instance of the current class.

 

What i don't understand is what is the purpose of using $db->getConnection???

 

 

Link to comment
https://forums.phpfreaks.com/topic/282325-question-about-using-mysqlli/
Share on other sites

  • Solution

Your database class doesn't provide any functionality for actually querying the database. The only thing it does is create and store a connection using mysqli. The getConnection method returns that connection so that your other code can then use it to actually do work with the database.

Make sure that the visibility of  __construct() is private not public.

The purpose of a singleton in this case is so that one and only one database connection will be established, having a public construct breaks that functionality.

Edited by DFulg

Make sure that the visibility of __construct() is private not public.

The purpose of a singleton in this case is so that one and only one database connection will be established, having a public construct breaks that functionality.

With that in mind though, be aware that the singleton pattern is often referred to as an anti pattern. It should really be avoided unless you understand why.

 

Lines like this:

 

$db = Database::getInstance();
within another class are a good example of why a singleton is bad news. It has tightly coupled your class with some Database class that it is expecting to always exist within the global namespace.

I agree fully with trq. Singletons are usually bad news because they blow apart encapsulation and scope, which are two of the underpinnings of OOP. I'm generally weary about database wrappers anyway as many are just a layer of unneeded abstraction.

 

In any event, instead of using a Singleton, look into Dependency Injection (DI, also known as Inversion of Control (IoC)). It's a much cleaner way to introduce external dependencies to an object, and it does it without knocking down the walls that define an object's boundaries.

I just want to add I find it funny for I was reading that same tutorial and after discussion about DI, I found this http://pimple.sensiolabs.org/ while doing a Google search. When I get time I am going to use Pimple and convert it over to that instead of the database wrapper that I'm currently using.

 

Sorry I don't mean to hijack this thread, just trying to help. I definitely like PHP Freaks for you definitely learn something new everyday.

Edited by Strider64

Thanks Kicken. For I second I thought the Database::getInstance() took an instance of the address class, but after re-reading the code it took an instance of the DB Class.

 

In regards to the other response. I am not sure what a singleton pattern it but I will defiantly read that!

Well Id recommend against using singleton database connection, but if you are using it as a learning process for programming/OOP its perfectly fine. They really are not OOP, but its still good to know how to deal with them as you may encounter programs from other coders that use statics/singleton if you work in a team. Still, keep in mind that static methods/singleton methods are poor OOP practices, use them sparsingly.

Edited by Hall of Famer
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.