1
$db = new PDO("mysql:host=".DBHOST.";dbname=".DBNAME, DBUSER, DBPASS);

function abc(){
    global $db;
    ...
}

function xyz(){
    global $db;
    ...
}

Let's say I have 100 functions which uses $db.

Is it the true that I must 100 times write global $db?

Is there a way to access $db in all functions automatically, like in javascript?

qadenza
  • 9,025
  • 18
  • 73
  • 126

5 Answers5

1

You can otherwise use super global $GLOBALS['db'] directly inside your function without the need to declare every time.

Only for this reason we use Object Oriented Approach or some libraries like medoo.


Update: (From one of your comments)

You nay need to understand the Scope of the Variable to know its advantage before calling so.

Thamilhan
  • 13,040
  • 5
  • 37
  • 59
  • but writing `$GLOBALS['db']` is even more then writing `global $db`. – qadenza Aug 14 '17 at 05:26
  • 1
    Yes, but declaration prior to its usage inside the function can be skipped. But that is the reason why would I suggest you for some object oriented approach using class – Thamilhan Aug 14 '17 at 05:28
1

as far I know, there no way to do such thing, you need to declare global $db in every function you wanna use it

Sage Harpuia
  • 348
  • 2
  • 13
  • @bonaca it's a restriction, a feature. If they allow developers to use global variables inside function scope or class scope without the keyword 'global', there is a high chance we (developers) would mix between local scope variables and global variables – evilReiko Aug 14 '17 at 05:30
  • javascript solved this very simply: global variables inside a function is accessed by writing without `var`. – qadenza Aug 14 '17 at 05:34
  • @bonaca Client-side language != Server-side language. No comparisons. Read the updated [answer](https://stackoverflow.com/a/45667849/5447994) – Thamilhan Aug 14 '17 at 05:36
  • Difference between `server side` and `client side` is just about execution platform.I think - there is no reason to limit variable access in a language just because it's a `server-side`. – qadenza Aug 14 '17 at 05:40
  • @bonaca every language has its ups and downs, and every language has its own way of how to handle specific things, like this question for example. If we will compare languages about this question, we will no-doubt find that every language handles this question differently – evilReiko Aug 14 '17 at 05:43
  • I would say that this `variable stuff` is a php's `very deep down` and I'm waiting someone to say that clearly. – qadenza Aug 14 '17 at 05:51
  • @bonaca In JS, it's easier/shorter than PHP by a line, but it's also extremely easy to go into hell of a nightmare by mistakenly overwriting global variables. And because of this, [there are hundreds of topics why it's bad & evil to declare global variables/methods and how to avoid them](https://stackoverflow.com/a/2613647/134824). – evilReiko Aug 14 '17 at 08:31
1

You can pass $db as parameter to functions or use $GLOBALS['db']

function abc($db){
    $db->query();
}

or

$GLOBALS['db'] = new PDO("mysql:host=".DBHOST.";dbname=".DBNAME, DBUSER, DBPASS);
function abc (){
   $GLOBALS['db']->query();
}
aidinMC
  • 1,415
  • 3
  • 18
  • 35
1

If need to access variable, then yes, you have to add global in all your functions or use $_GLOBALS

since you are using function, better to create static class

class DB {
   private static $db       = null;
   public static function connect() {
     if(!empty(self::$db)) {
       return self::$db;
     }
     self::$db = new PDO("mysql:host=".DBHOST.";dbname=".DBNAME, DBUSER, DBPASS);
    }
}

Access db using DB::connect()->query();

Rajapandian
  • 216
  • 2
  • 7
1

As everyone already said, you have to use the keyword global to avoid mixing with local variables:

function my_func() {
    global $db;
    ...
}

If you really need a global variable, one possible way is to use the $_GLOBALS:

$_GLOBALS['db'] = $db;    

function my_func() {
    $_GLOBALS['db']->...;
    ...
}

It's better to use OOP, but sometimes, a developer needs to write a quick script that do stuff, and PHP is a good choice for this. If you don't want to go OOP-way and still want to use global variables in many functions, here's a workaround.

1- Create a file, let's call it my_globals.php that has this content:

// my so many globals stay here
global $db;
global $a;
global $b;
...

2- In your main file, you would do something like this:

$db = new PDO("mysql:host=".DBHOST.";dbname=".DBNAME, DBUSER, DBPASS);
$a = ...;
$b = ...;

function abc(){
    include 'my_globals.php';// this will be equivalent as typing all your global variables
    ...
}

function xyz(){
    include 'my_globals.php';// this will be equivalent as typing all your global variables
    ...
}
evilReiko
  • 19,501
  • 24
  • 86
  • 102
  • Thanks, but it's much simpler to write `global $db` each time. – qadenza Aug 14 '17 at 05:44
  • True. I suggested this workaround if you have **many** global variables, in that case, in every function, you will to write multiple of `global $var` – evilReiko Aug 14 '17 at 05:46