Hi I'm trying to use the functions/methods of one class inside another, however I keep getting errors and I don't understand this...

for example;

class DB {

private $host;
private $user;
private $pass;
private $database;

public function __construct($host, $user, $pass, $database) {

public function query($sql) {


$db = new DB;

class Permissions {

global $db;

public function update($user_id) {
$this->db->query("UPDATE `users` SET `user_name` = 'Dom'....");

9 Years
Discussion Span
Last Post by Jord09

It probably can't connect to the DB... Unless the DB class uses your login credentials as default.

$db = new DB('localhost', 'youruser', 'yourpass', 'yourdb');

might do the trick, although this solution is not recommended.


thats not the problem, I cut bits out of the code to make it clearer. DB connection works. Just trying to access class methods outside of the class.

"syntax error, unexpected T_GLOBAL, expecting T_FUNCTION"

"Can't use function return value in write context"

Edited by Jord09: n/a


I think I found a solution and that is to reference the other class llike so

Class {

public $db;

public function __construct() {

$this->db = &new DB;



This works with no problems, thanks anyways.

Edited by Jord09: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.