Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a singleton database class in functions and multiple scripts(PHP) - best use methods

I have a singleton db connection which I get with:

$dbConnect = myDatabase::getInstance();

which is easy enough. My question is what is the least rhetorical and legitimate way of using this connection in functions and classes? It seems silly to have to declare the variable global, pass it into every single function, and/or recreate this variable within every function. Is there another answer for this?

Obviously I'm a noob and I can work my way around this problem 10 different ways, none of which is really attractive to me. It would be a lot easier if I could have that $dbConnect variable accessible in any function without needing to declare it global or pass it in. I do know I can add the variable to the $_SERVER array...is there something wrong with doing this? It seems somewhat inappropriate to me.

Another quick question: Is it bad practice to do this:

$result = myDatabase::getInstance()->query($query);

from directly within a function?

like image 813
dscher Avatar asked Mar 12 '10 07:03

dscher


People also ask

When to use singleton in Laravel?

Use singleton for a class or object that you need access to throughout the application - the object is only constructed once and so retains state throughout execution. If you only need a single, shared, instance of a class, in this situation use singleton.

Should DB class be singleton?

A DB connection should not normally be a Singleton. Two reasons: many DB drivers are not thread safe. Using a singleton means that if you have many threads, they will all share the same connection.

What is singleton class in PHP?

Object Oriented ProgrammingPHPProgramming. Singleton Pattern ensures that a class has only one instance and provides a global point to access it. It ensures that only one object is available all across the application in a controlled state.

What is a singleton in programming?

A singleton is a class that allows only a single instance of itself to be created and gives access to that created instance. It contains static variables that can accommodate unique and private instances of itself. It is used in scenarios when a user wants to restrict instantiation of a class to only one object.


1 Answers

Some of this will come down to taste. At work we also obtain an ADODB connection handle via

$db = Registry :: getDB();

Likewise you can shorthand some of this code by using method chaining:

$name = Registry :: getDB()->getOne(
    "SELECT name FROM user WHERE user_id = ?", $userId
);

Using a registry means your calling code has a concrete dependency on your Registry class. This makes testing your code difficult (personally this is the only time I've banged my head on the wall using a Registry).

If you want to add unit tests for your code, you'd often want to mock your database connection so you don't manipulate the database each time you run your test suite.

You can still get round this by examining your running environment and configuring your registry with a mock database abstraction class but it's not as elegant as dependency injection (DI).

The best explanation of DI and containers (for a PHP developer) I've seen are Fabien Potencier's Dependency Injection with PHP 5.3 (starting on slide 9).

You pass a DI container around in place of a Registry, it provides an elegant and easy way to obtain handles to dependencies (like the Registry) but is flexible and more loosely coupled so you can mock those dependencies when needed.

like image 175
Greg K Avatar answered Nov 14 '22 22:11

Greg K