title: Fatal error: Uncaught Error: Call to a member function select() on null
body: <p>I am kinda new to OOP in php and I am facing this problem every time in the model but I can't find the error causing this. </p>
  
  <p>I get an error saying: Fatal error: Uncaught Error: Call to a member function select() on null in my <strong>model</strong> in the getCategories function (See below code).</p>
  
  <pre><code>&lt;?php
  
  class model_additem extends model{
      public function __construct(){
          DB::setConnection(DB_ADMIN,DB_ADMIN_PASS);
      }
  
      public function insertItem($picName,$n,$d,$p,$ui,$ic,$pt,$pd="good"){
          $data = ["pic_name"=&gt;$picName,"pic_desc"=&gt;$pd,"pic_type"=&gt;$pt];
          $img = parent::$db-&gt;saveTo("pictures")-&gt;setData($data)-&gt;execute();
  
          if($img){
              $imgId = parent::$db-&gt;lastInsertId();
  
              $data = ["name"=&gt;$n,"description"=&gt;$d,"price"=&gt;$p,"image_id"=&gt;$imgId,"owner_id"=&gt;$ui,"category_id"=&gt;$ic];
              $prod = parent::$db-&gt;saveTo("products")-&gt;setData($data)-&gt;execute();
              return $prod;
          }
          return false;
      }
  
      public function getCategories(){
          $data = parent::$db-&gt;select()-&gt;from('category')-&gt;fetch('all');
          if($data) return $data;
          return false;
      }
  
  
  }
  </code></pre>
  
  <p>And here is my <strong>CONTROLLER</strong> file:</p>
  
  <pre><code>&lt;?php
  
  class controller_additem extends controller{
      public function __construct(){
          Load::view("admin".DS."header");
          Load::view('additem/additem');
          Load::view("admin".DS."footer");
          require_once 'CheckAdmin.php'; // Check if the user is admin to enter page + more.
  
  
      }
  
      public function index(){
          $item = $this-&gt;model("additem");
          if(isset($_POST["addItem"])){
              $iName = filter_input(INPUT_POST,"iName",FILTER_SANITIZE_MAGIC_QUOTES);
              $iDesc = filter_input(INPUT_POST,"iDesc",FILTER_SANITIZE_MAGIC_QUOTES);
              $iPrice = filter_input(INPUT_POST,"iPrice",FILTER_SANITIZE_NUMBER_FLOAT);
              $iCat = filter_input(INPUT_POST,"category",FILTER_SANITIZE_NUMBER_INT);
              $iPic = $_FILES["iPicture"];
  
              $extention = explode(".",$iPic["name"]);
              $ext = end($extention);
              //pre($iPic);
              //validation
              //if($iPic["size"]&gt; 2000000){}
              $picName = $iName.time().'.'.$ext;
              if(move_uploaded_file($iPic["tmp_name"],ROOT . "public/images/" . $picName)){
                  $userId = Session::get("userId");
  
                  $result = $item-&gt;insertItem($picName,$iName,$iDesc,$iPrice,$userId,$iCat,$ext);
                  if($result){
                      Session::set("result","Item has been inserted successfully");
                  }else{
                      Session::set("result","Error occured");
                  }
  
              }
          }
          $cats = $item-&gt;getCategories();
          $uName = Session::get("userData")["name"];
          Load::view("additem/additem",$cats);
      }
  }
  </code></pre>
  
  <p>I also have a db class in my engine file that handles functions related to database and the select function is in there:</p>
  
  <pre><code>class db{
      private static $db = null;
      private $sql = "";
      private $save_type;
      private $binded = array();
      private $query = '';
      private $last_insert_id;
  
      public function __construct(){
          if(self::$db == null){
              $user = DB_GUEST;
              $pass = DB_GUEST_PASS;
              if(Session::found("user_priv") == 'no'){
                  $user = DB_USER;
                  $pass = DB_USER_PASS;
              }
              if(Session::found("user_priv") == 'yes'){
                  $user = DB_ADMIN;
                  $pass = DB_ADMIN_PASS;
              }
              self::setConnection($user,$pass);
          }
      }
  
      public static function setConnection($user,$pass,$dbname=DB_NAME){
          try{
              self::$db = new PDO("mysql:host=localhost;dbname=$dbname",$user,$pass);
              self::$db-&gt;setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
              self::$db-&gt;setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
              self::$db-&gt;exec("SET NAMES utf8");
          }catch(PDOException $e){
              die($e-&gt;getMessage());
          }
      }
  
      public static function getDb(){
          if(self::$db == null){
              $user = DB_GUEST;
              $pass = DB_GUEST_PASS;
              if(Session::found("userId")){
                  $user = DB_USER;
                  $pass = DB_USER_PASS;
              }
              if(Session::found("admin")){
                  $user = DB_ADMIN;
                  $pass = DB_ADMIN_PASS;
              }
              self::setConnection($user,$pass);
          }
          return self::$db;
      }
  
      //SELECT [name,pass,email]
      public function select(array $select=[]){
          $sql = "SELECT ";
          if(empty($select)){
              $sql .= "*";
          }else{
              filter_array($select);
              $select = implode(",",$select);
              $sql .= string($select);
          }
          $this-&gt;sql = $sql;
  
          return $this;
      }
  
      //FROM
      public function from($tbl){
          $this-&gt;sql .= " FROM " . string($tbl);
          return $this;
      }
  
      //WHERE
      public function where(array $where){
          //filter_array($where);
          $sql = " WHERE ";
          foreach($where as $key =&gt; $val){
              $sql .= $key . " LIKE '".$val."' AND ";
          }
  
          $this-&gt;sql .= rtrim($sql," AND ");
          return $this;
      }
  
      //JOIN
      public function join(array $tbl,$type="LEFT JOIN"){
          $sql = " ".strtoupper($type)." ";
          $sql .= implode(",",$tbl);
  
          $this-&gt;sql .= $sql;
          return $this;
      }
  
      //ON
      public function on($cond1,$cond2){
          $sql = " ON ";
          $sql .= string($cond1) . " = " . string($cond2);
  
          $this-&gt;sql .= $sql;
          return $this;
      }
  
      //ORDER BY
      public function order_by($order_by, $order_type="ASC"){
          $sql = " ORDER BY ";
          $sql .= string($order_by)." ".strtoupper(string($order_type));
  
          $this-&gt;sql .= $sql;
          return $this;
      }
  
      //LIMIT
      public function limit($limit){
          $this-&gt;sql .= " LIMIT " . int($limit);
          return $this;
      }
  
      //FETCH
      public function fetch($fetch_type=""){
          $fetch = "fetch";
          if($fetch_type){
              $fetch = "fetchAll";
          }
  
          try{
              $query = self::$db-&gt;query($this-&gt;sql);
              $this-&gt;reset();
              return $query-&gt;$fetch();
          }catch(PDOException $e){
              die($e-&gt;getMessage());
          }
      }
  
      //saveTo
      public function saveTo($tbl,$type="insert"){
          $tbl = string($tbl);
          $this-&gt;save_type = strtolower(string($type));
  
          if($this-&gt;save_type == "update"){
              $sql = "UPDATE ";
          }elseif($this-&gt;save_type == "replace"){
              $sql = "REPLACE INTO ";
          }else{
              $sql = "INSERT INTO ";
          }
  
          $sql .= $tbl . " SET ";
          $this-&gt;sql = $sql;
          return $this;
      }
  
      //SET DATA
      public function setData(array $data,$filter="string"){
          $filter = string($filter);
  
          foreach($data as $colomn =&gt; $value){
              $colomn = string($colomn);
              $value = $filter($value);
              $this-&gt;binded[$colomn] = $value;
              $this-&gt;sql .= $colomn . "=:" . $colomn . ",";
          }
          //pre($this-&gt;binded);
          $this-&gt;sql = rtrim($this-&gt;sql,",");
          return $this;
      }
  
      //execute
      public function execute(){
          try{
              $this-&gt;query = self::$db-&gt;prepare($this-&gt;sql);
              if($this-&gt;binded){
                  foreach($this-&gt;binded as $colomn =&gt; $value){
                      $this-&gt;bind($colomn,$value);
                  }
              }
              $result = $this-&gt;query-&gt;execute();
  
              if($this-&gt;save_type == 'insert'){
                  $this-&gt;last_insert_id = self::$db-&gt;lastInsertId();
              }
              $this-&gt;reset();
              return $result;
  
          }catch(PDOException $e){
              die($e-&gt;getMessage());
          }
      }
  
      //BIND
      private function bind($placeholder,$value,$filter="string",$bind_type="bindValue"){
          return $this-&gt;query-&gt;$bind_type(":".$placeholder,$filter($value),PDO::PARAM_STR);
      }
  
      public function lastInsertId(){
          return $this-&gt;last_insert_id;
      }
  
      //DELETE
      public function delete($tbl,$col=false){
          $this-&gt;sql = "DELETE ";
          if($col){
              $this-&gt;sql .= string($col)." ";
          }
          $this-&gt;sql .= "FROM " . string($tbl);
          return $this;
      }
  
      //TRUNCATE
      public function truncate($tbl){
          $this-&gt;sql = 'TRUNCATE ' . string($tbl);
          return $this;
      }
  
      //View Query
      public function viewQ(){
          echo $this-&gt;sql;
      }
  
      //RESET
      private function reset(){
          $this-&gt;sql = "";
          $this-&gt;save_type = "";
          $this-&gt;query = "";
          $this-&gt;binded = array();
      }
  }
  </code></pre>
  
  <p>The thing is that if you look in the model at the saveTo function, it appears to work. However, select doesn't nor the functions that comes after it.</p>
  
  <p>Please help me, I tried to search for this error but couldn't find the solution for my particular problem. </p>
  
  <p>Any help is highly appreciated!
  Thanks in advance.</p>
  
  <p><strong>EDIT</strong>: Thanks everybody for your help! I figured it out. There was a mistake in the model class so the there wasn't connection with the db.</p>