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'....");
}

}

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"

Apparantly global is not allowed within a class definition.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.