eldan88 Posted September 20, 2013 Share Posted September 20, 2013 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??? Quote Link to comment https://forums.phpfreaks.com/topic/282325-question-about-using-mysqlli/ Share on other sites More sharing options...
Solution kicken Posted September 20, 2013 Solution Share Posted September 20, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/282325-question-about-using-mysqlli/#findComment-1450469 Share on other sites More sharing options...
DFulg Posted September 20, 2013 Share Posted September 20, 2013 (edited) 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 September 20, 2013 by DFulg Quote Link to comment https://forums.phpfreaks.com/topic/282325-question-about-using-mysqlli/#findComment-1450485 Share on other sites More sharing options...
trq Posted September 21, 2013 Share Posted September 21, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/282325-question-about-using-mysqlli/#findComment-1450545 Share on other sites More sharing options...
KevinM1 Posted September 21, 2013 Share Posted September 21, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/282325-question-about-using-mysqlli/#findComment-1450577 Share on other sites More sharing options...
Strider64 Posted September 21, 2013 Share Posted September 21, 2013 (edited) 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 September 21, 2013 by Strider64 Quote Link to comment https://forums.phpfreaks.com/topic/282325-question-about-using-mysqlli/#findComment-1450602 Share on other sites More sharing options...
eldan88 Posted September 22, 2013 Author Share Posted September 22, 2013 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! Quote Link to comment https://forums.phpfreaks.com/topic/282325-question-about-using-mysqlli/#findComment-1450615 Share on other sites More sharing options...
Hall of Famer Posted September 22, 2013 Share Posted September 22, 2013 (edited) 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 September 22, 2013 by Hall of Famer Quote Link to comment https://forums.phpfreaks.com/topic/282325-question-about-using-mysqlli/#findComment-1450617 Share on other sites More sharing options...
eldan88 Posted September 22, 2013 Author Share Posted September 22, 2013 Will do. Thanks a lot Hall of Famer! Quote Link to comment https://forums.phpfreaks.com/topic/282325-question-about-using-mysqlli/#findComment-1450627 Share on other sites More sharing options...
moja Posted September 25, 2013 Share Posted September 25, 2013 Can you link me to the mysql tutorial? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/282325-question-about-using-mysqlli/#findComment-1451223 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.