moderadorkl25
moderadorkl25

Reputation: 213

using $mysql used in class in all functions

I have a small question regarding the use of Global. I downloaded a Query class, and changed/added several things including switched to mysqli, finally there is some different roles that need to link mysql to another page with variable settings.

Finally the functions of the class, need to call the variable $ mysqli, so I have to declare at the start of the function global $mysqli;

There is a way to declare this variable for all functions at once page?

Upvotes: 0

Views: 51

Answers (1)

user1864610
user1864610

Reputation:

You really shouldn't use global for this. You should pass the $mysqli object in as a parameter to the constructor ad refer to $this->mysqli from within your methods.

For example:

class myDb {

private $mysqli;

function __construct($mysqli) {
  $this->mysqli = $mysqli;
}

function queryDb($query) {
    return $this->mysqli->query($query);
}
}

Upvotes: 1

Related Questions