0

I'm tring to access to a global variable assigned by a SESSION element.
I take back this error

Parse error: syntax error, unexpected 'global' (T_GLOBAL), expecting function (T_FUNCTION)

<?php

$DBAdmin = $_SESSION['dbendpoint'];

class ConnessioneDB
{
    global $DBAdmin;
    var $MySQLHost = "localhost";
    var $MySQLUser = "root";
    var $MySQLPass = "";
    var $MySQLDB   =  $DBAdmin;
    var $conn;

I need to access to $DBAdmin variable to create mysqli istance. Thank you for support.

Tech Spot
  • 464
  • 3
  • 10

1 Answers1

0

The problem here is that throwing "global" just inside of a class is simply not valid php. Instead, try declaring it as a class variable, and then assign a value to it in the constructor (or in another function, as you see fit):

class ConnessioneDB
{
    var $DBAdmin;
   // ...

    function __construct() {
         global $DBAdmin;
         $this->DBAdmin= &$DBAdmin;
    }
Andrew
  • 827
  • 2
  • 6
  • 14
  • 1
    Or, better yet, follow best practices and not use the `global` keyword at all and pass it in as a parameter instead. – John Conde Aug 24 '19 at 12:13
  • I see your point, and don't disagree with it. But a database connection is probably a basic enough functionality that you shouldn't have to plan on passing it every single time you want to use classes. I think this solution is likely good enough for OP at this point, but in the future, I'd recommend them to look into how some MVC frameworks deal with stuff like this through extensions and such. – Andrew Aug 24 '19 at 12:44