0

I've got a big problem:

I'm writing a new WebApp without a Framework. I'm using xampp and sublime text. I dont know how can i solve this problem.

An example how my DB.php written

class DB{

    private static  $_baglan = null;
    private $_pdo,
            $_query,
            $hatalar = false,
            $sonuc,
            $_sayac = 0;

            public function __construct(){
                try{

                    $this -> _pdo = new PDO('mysql:host=' . Config::getir('mysql/host') . ';dbname=' . Config::getir('mysql/db'), Config::getir('mysql/kullanici_adi'), Config::getir('mysql/sifre') );
                    // echo 'baglandi';
                }catch(PDOException $e){
                    die($e->getMessage());
                }
            }

            public static function baglan(){
                if (!isset(self::$_baglan)) {
                    self::$_baglan = new DB();
                    // echo 'baglandi';
                }
                return self::$_baglan;
            }

            public static function query($sql, $parametre=array()){

                $this->_hatalar = false;  // line 32
                if ($this->_query = $this->_pdo->prepare($sql)) {
                    $x = 1;
                    if (count($parametre)) {
                        foreach ($parametre as $param) {
                            $this->_query->bindValue($x, $parametre);
                            $x++;
                        } 
                    }
                    if ($this->_query->execute()) {
                        $this->sonuc=$this->_query->fetchAll(PDO::FETCH_OBJ);
                        $this->_sayac=$this->_query->rowCount();
                    }else{
                        $this->_hatalar=true;
                    }
                }
                return $this;
            }

            public function eylem($eylem, $tablo, $where=array()){

                if (count($where)===3) {

                    $operatorler = array('=', '<', '>', '>=', '<=');
                    $alan = $where[0];
                    $operator = $where[1];
                    $deger = $where[2];
                    if (in_array($operator, $operatorler)) {
                        $sql = "{$eylem} FROM {$tablo} WHERE {$alan} {$operator} ?";
                        if (!$this->query($sql, array($deger))->hatalar()) {
                            return $this;
                        }
                    }
                }
                return false;
            }

            public function getir($tablo, $where){
                return $this->eylem('SELECT *', $tablo, $where);
            }
            public function sil($tablo, $where){
                return $this->eylem('DELETE', $tablo, $where);
            }

            public function hatalar(){
                return $this->hatalar();
            }

}

In my index.php I'm loading maybe

require_once 'core/init.php';

// echo Config::getir('mysql/host');
// calismadi $kullanici = DB::baglan() -> query("SELECT kullanici_adi FROM uye WHERE kullanici_adi = ?", array('oguzhan'));
$kullanici = DB::baglan()->getir('uye', array('kullanici_adi', '=', 'oguzhan'));

if ($kullanici->hatalar()) {
    echo 'Kullanıcı yok';
}else{
    echo 'Var';
}

Why is the error coming?

Pang
  • 9,564
  • 146
  • 81
  • 122

1 Answers1

0

Your problem can be solved by removing the static keyword from the query method.

You can only use $this on instantiated objects. self or static references the class itself, not any instantiated objects of the class.

class Person{

    private $name;

    public function __construct(string $name){
        $this->name = $name;
    }

    public function greet(){
        echo 'Hello my name is ' . $this->name;
    }
}

$person = new Person('Thomas');
$person->greet(); // prints 'Hello my name is Thomas'

$this works in this example because we first instantiated the object using the new keyword. We are also not using the static keyword on the of the methods and variables.

self and static act only on the class itself and not on the instantiated object. The difference between self and static is explained here.

Here is an example of self usage, notice the use of the static keyword on the methods and variables of the class:

class StaticClass{
    private static $variable = '';
    public static function change(string $variable){
        self::$variable = $variable;
    }
    public static function print(){
        echo self::$variable;
    }
}

StaticClass::change('Hello');
StaticClass::print(); // print 'Hello'
Community
  • 1
  • 1
Thomas
  • 537
  • 1
  • 4
  • 14