Jump to content

Class Problem


POG1

Recommended Posts

I have 2 separate classes, database and user class.

 

Inside the user class I have to do a query and if i try to use the db class it produces an error, is there any way i can make this work?

 

I have tried things but with no success.

 

class User { 

protected $id;
public function getDetails() { 

	$this->id = (int) $_SESSION['user_id'];
	$query = 'SELECT `Username` FROM `' . DBPREFIX . 'users` WHERE `ID` = ' . $db->qstr ( $id ) ;

	// do stuff?

	return true;

}

}

 

The error it says is "Fatal error: Call to a member function qstr()"

 

Link to comment
Share on other sites

You can pass $db as a param to the method getDetails() or pass it via the constructor when you instantiated the class.

 

Pass as param:

class User {
     ...
     public function getDetails ($db) {
          ... $db ...
     }
}
$db = ...;
$user = new User;
$user->getDetails($db);

 

Via constructor:

class User {
     private $db;
     public function __construct ($db) {
           $this->db = $db;
     }
     public function getDetails () {
          ... $this->db ...
     }
}
$db = ...;
$user = new User ($db);
$user->getDetails();

 

Does that help?

Link to comment
Share on other sites

It's kind of odd the way you're writing it. The reason it doesn't work is because $id is undefined. You have to reference the class's $id with $this->id; however, as I said, it's an odd way to set $this->id each time.

 

Try it this way.

class User {
     private $db;
     protected $id;
     public function __construct ($db, $id) {
          $this->db = $db;
          $this->id = intval($id);
     }
     public function getDetails() {
          $query = 'SELECT `Username` FROM `' . DBPREFIX . 'users` WHERE `ID` = ' . $db->qstr ( $this->id ) ;
          // do stuff?
          return true;
     }
}

$db = ...;
$id = $_SESSION['user_id'];
$user = new User($db, $id);
$user->getDetails();

Link to comment
Share on other sites

Oh oops.

 

Try this

class User {
     private $db;
     protected $id;
     public function __construct ($db, $id) {
          $this->db = $db;
          $this->id = intval($id);
     }
     public function getDetails() {
          $query = 'SELECT `Username` FROM `' . DBPREFIX . 'users` WHERE `ID` = ' . $this->db->qstr ( $this->id ) ;
          // do stuff?
          return true;
     }
}

$db = ...;
$id = $_SESSION['user_id'];
$user = new User($db, $id);
$user->getDetails();

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.