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
https://forums.phpfreaks.com/topic/158962-class-problem/
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
https://forums.phpfreaks.com/topic/158962-class-problem/#findComment-838387
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
https://forums.phpfreaks.com/topic/158962-class-problem/#findComment-838403
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
https://forums.phpfreaks.com/topic/158962-class-problem/#findComment-838541
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.