PHP Libraries - $db no longer GLOBALLY available in 1.6.2 ?

PHP Libraries - $db no longer GLOBALLY available in 1.6.2 ?

dmcglynndmcglynn Posts: 15Questions: 5Answers: 0

After upgrading to 1.6.2, I suddenly was presented with the following error when refreshing the page:

Fatal error: Call to a member function transaction() on null in
\datatables\Editor\Editor.php on line 851

After some looking under the hood and online, I see that in Bootstrap.php, the bottom of the file is now missing the line which actually makes it a global variable:

//
// Database connection
//   Database connection is globally available
//
$db = new Database( $sql_details );

I still had the file from 1.6.1, so I compared the two, and it's just missing in 1.6.2.

Did anyone else run into this issue, or was I supposed to get away from using a global $db variable a long time ago :smile:

Replies

  • allanallan Posts: 61,438Questions: 1Answers: 10,049 Site admin

    That's odd. I've just downloaded the 1.6.2 package and the Bootstrap.php file does indeed include those lines and two empty new lines at the end of the file.

    I'm not sure I can explain that one. I assume it was from the download page that you got the 1.6.2 package?

    Allan

  • dmcglynndmcglynn Posts: 15Questions: 5Answers: 0

    Hi Allan,

    First of all, sorry it took me so long to respond. And now I'm second-guessing myself - I'm thinking that I committed the 'change the library code' sin, a long time ago during debug, and don't remember doing so. I had

    global $db;
    

    right above the 'new database' line on my copy of 1.6.1. I don't remember doing this, but it would have been a long time ago if I did do it..

    The comment says it is global, but given that the virgin code from the download doesn't have what my file did, I assume I should be declaring it global somewhere else?

    Sorry if this is confusing, and thanks for the help.
    David

  • allanallan Posts: 61,438Questions: 1Answers: 10,049 Site admin

    The global keyword in PHP is one way in which you can access a global variable inside a function. Unlike Javascript PHP doesn't automatically expose variables from an outer scope in the function. You have to explicitly tell it to do that.

    Using global outside of a function, as far as I am aware, doesn't mean anything in PHP (happy to be proven wrong!).

    Allan

  • tangerinetangerine Posts: 3,342Questions: 35Answers: 394
    edited May 2017

    Coincidentally, I ran into this yesterday. Although DataTables,php and Bootstrap.php are correctly "include"-ed at the top of my Controller class file, the $db variable is not available inside the Controller class's method.
    Declaring "public $db;" inside the class doesn't fix it.
    I suspect I'm missing something simple about scoping, but while I'm investigating, does any one have a work-around for this?

  • allanallan Posts: 61,438Questions: 1Answers: 10,049 Site admin

    You'd need to either access it from $_GLOBALS['db'], or much better would be to pass it into the class constructor and it would save to a private parameter.

    Allan

  • dmcglynndmcglynn Posts: 15Questions: 5Answers: 0

    Allan, you're absolutely right - it took a good night's sleep to realize that with this framework that I'm new to using, the place I'm actually 'include'-ing datatables.php from is within a function, so it isn't being set global by default./

    I'm now looking at including it via composer (I'm also new to that library!) as a bunch of other libraries are already loading that way.

  • tangerinetangerine Posts: 3,342Questions: 35Answers: 394

    Thanks Allan - $GLOBALS does it (applied at both ends). I would prefer direct injection, but that task will have to join the queue.

This discussion has been closed.