Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] Using $this when not in object context
Turson
post
Post #1





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

Ostrzeżenie: (0%)
-----


Z poniższego kodu otrzymuję błąd Fatal error: Using $this when not in object context in .. on line (przygotowanie zapytania do bazy)

Klasa
  1. <?PHP
  2. class User extends Connect{
  3. static function isActivate($user_id){
  4. $stmt=$this->db->prepare("SELECT activ_key FROM keys WHERE user_id=:user_id");
  5. $stmt->bindValue(':user_id', $user_id, PDO::PARAM_INT);
  6. $stmt->execute();
  7. if($stmt->rowCount()==0){
  8. return true;
  9. }
  10. }
  11. }
  12. ?>


Odwołanie do klasy
  1. if(User::isActivate($_SESSION["user_id"]))


Klasa połączenia z bazą
  1. <?PHP
  2. class Connect{
  3. public function __construct() {
  4. $this->db = new PDO('mysql:host=localhost;dbname=x', 'x', 'x', array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'")) or die();
  5. }
  6. }
  7. ?>


Popszperałem w sieci i znalazłem odpowiedź, że w metodach static zamiast $this używamy self::
więc zmodyfikowałem na
  1. $stmt=self:(IMG:style_emoticons/default/snitch.gif) db->prepare("SELECT activ_key FROM keys WHERE user_id=:user_id");

i wtedy błąd
Cytat
Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR)
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 25.08.2025 - 05:44