-2
<?php
$app = [];
$app['config'] = require 'config.php';
require 'core/helpers.php';
require 'core/util.php';
require 'core/Router.php';
require 'core/Request.php';
require 'core/database/Connection.php';     
require 'core/database/QueryBuilder.php';
require 'core/database/Account.php';
require 'models/Campaign.php';

This is how I code the Connection.php

<?php

class Connection
{
    public static function make($config)
    {
        try {
          return new PDO(
                $config['connection'].';dbname='.$config['name'],
                $config['username'],
                $config['password'],
                $config['options']
            );
        } catch (PDOException $e) {
            die($e->getMessage());
            // exit();
        }
    }
}

and in my QueryBuilder.php or Account.php file, i refer to it like

class QueryBuilder {
    protected $pdo; 

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

this specific query really is the problem here that's why I'm stucked ...

    public static function check_if_the_email_is_currently_registered($pin){
        $sql = "SELECT COUNT(*) FROM users WHERE email = :pin";
        $statement = $this->pdo->prepare($sql);     
        $statement->execute();
        $count =  $statement->fetch(PDO::FETCH_NUM) ; 
        return reset($count); 
    }

I get this error

Fatal error: Uncaught Error: Using $this when not in object context

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
TheBAST
  • 2,680
  • 10
  • 40
  • 68

1 Answers1

-1

$this refer to the object instance. when you use static keyword you're referring to the Class level not to the instance level which is why u get the error. Remember you can call your static function without creating the Object. but you're static function use the pdo object which is being created when you create the object QueryBuilder (by using __constructor). hope you figured it out from here.

cute_programmer
  • 332
  • 3
  • 13