2

I have variable, $db and more variables. I have a lot of includes, that in the beginning of the files, have

global $db;

and I want to define the variable $db and some more variables as global and remove the global from the beginning of the includes, that I'll have only the definition on the index file and not in the includes. for example: index.php:

$db = new DataBase;
global $db;
include "file.php";

file.php:

$db->select("...");

now file.php have "global $db;" for it will work, but I want to remove it, and leave only in the index.php

thanks for helping

user3742451
  • 65
  • 1
  • 5
  • are you planning to do the singleton practice? http://stackoverflow.com/questions/8776788/best-practice-on-php-singleton-classes – Netorica Aug 26 '14 at 08:49
  • 1) Globals are bad. 2) A variable is already global, unless it's defined in a function or the file is included inside a function. – deceze Aug 26 '14 at 08:54
  • You don't need any global declarations here. If you remove both the `global` declarations (in both `index.php` and `file.php`) then `index.php` will work correctly if `file.php` is included. (But `file.php` won't work on its own - with or without `global`.) – Stefan Aug 26 '14 at 08:56
  • 1
    @deceze: An external variable is NOT already global if you want to use it inside a function where it is not defined. – Stefan Aug 26 '14 at 08:58
  • Related: http://stackoverflow.com/questions/1234445/can-i-make-a-variable-globally-visible-without-having-to-declare-it-global-in-ev?rq=1 – Stefan Aug 26 '14 at 09:01
  • @Stefan A variable which is available *everywhere*, even in functions into which it has not been passed or included using the `global` keyword, are *superglobals*. They're different from *global variables*. Superglobals can not be created with userland code. – deceze Aug 26 '14 at 09:06
  • @deceze Yes, I (we) am talking about global variables, not superglobals. But you said `a variable (used in a function) is already global unless it is defined in the function`. This sounds wrong to me, but maybe I am just misinterpreting what you said. – Stefan Aug 26 '14 at 09:11
  • @Stefan No, I said the variables shown in the sample code are already global, even without the `global` keyword. *Unless* the file with `$db = new Database` is included inside a function, or that code is written inside a function. – deceze Aug 26 '14 at 09:29
  • @Stefan I tried it, and the error gives me is: Undefined variable: db. I'm running index.php and not file.php . It's matter if I'm using require_once or include? – user3742451 Aug 26 '14 at 09:33
  • @deceze: You literally said "a" variable is already global unless it's defined in a function. But I understand what you meant. @user3742451 Maybe the order of your declarations are wrong. `file.php` must be included after you declare `db`. Using `include` versus `require_once` should make no difference here. – Stefan Aug 26 '14 at 09:44
  • @Stefan Yes, I spoke in general terms, that *a* variable is global unless it is defined in *a* function. Anyway, glad that's resolved. :) – deceze Aug 26 '14 at 09:47
  • @deceze: OK - sorry for harping on about this: I initially thought you meant that a variable defined outside a function can be accessed inside a function without using `global` because it is already 'global'. But more correct thinking would be that strictly speaking all variables defined outside a function are already global for that function but you have to use the `global` keyword to access them inside the function. – Stefan Aug 26 '14 at 09:56
  • 2
    @Stefan Maybe our understanding of "global" differs. `global` is a special explicit scope in PHP, from which and to which variables can be pulled/pushed from anywhere (using the `global` keyword). To me it went without saying that a *function scope* does not automatically import all variables from the global scope. – deceze Aug 26 '14 at 09:59
  • @deceze You are correct. My thinking was very sloppy. For me, 'global' meant 'automatically imported into all scopes'. – Stefan Aug 26 '14 at 10:03

0 Answers0