-1

Confession: I read many similar questions on this platform, but nothing seems closely aligned to my situation. Most of the questions seem to originate from binding params in prepared statements or execute statement.
In my case, the website runs smooth on a local server(Apache2). However, it throws the error below when published on live server.

Fatal error: Uncaught Error: Call to a member function signin() on boolean in /storage/ssd5/815/17670815/app/controllers/signin.php:16 Stack trace: #0 /storage/ssd5/815/17670815/app/core/app.php(33): Signin->index() #1 /storage/ssd5/815/17670815/public_html/index.php(4): App->__construct() #2 {main} thrown in /storage/ssd5/815/17670815/app/controllers/signin.php on line 16

Context I'm using MVC (OOP) in PHP and here the relevant parts mentioned in the error. I hope this is not too much.

In the main index page, the line referred in the error is a core class(App) instantiation

<?php
session_start();
require_once '../app/initializer.php';
$app = new App(); //this is the line 4 mentioned in the error 

In Signin controller class the line referred in the error is indicated below

<?php

class Signin extends Controller{

    function index(){

        //you can do this if passing data to view
        $data["Page_title"] = "Signin";

        if($_SERVER['REQUEST_METHOD'] == "POST"){
           
            // this is a debuggin code
            //echo "I am signin controller <br />";
            // show($_POST);
            $user = $this->loadModel("User");
            $user->signin($_POST);  //this the line referred in the error
            
        }
        $this->view("zac/signin",$data);
        
    }


}

In class APP the line is a callback - check below

<?php

class App {
 
   private  $controller = "home";
   private  $method = "index";
   private  $params = [];
    
   public function __construct()
   {
     $url = $this->splitURL();
     if(file_exists("../app/controllers/".strtolower($url[0]).".php")){
        $this->controller = strtolower($url[0]);
       //unset the array position 
        unset($url[0]);

     }
     require "../app/controllers/".$this->controller.".php"; 
    //  echo file_get_contents('http://smart-ecom.000webhostapp.com/app/controllers/'.$this->controller.".php");
     
    //Create instance of whatever controller class is passed(if it exists, otherwise the home controller)
    $this->controller = new $this->controller;

    if(isset($url[1])){
      
      if(method_exists($this->controller, $url[1])){
        $this->method =$url[1];
        unset($url[1]);

      }
    }
    $this->params = array_values($url); 
    call_user_func_array([$this->controller, $this->method],$this->params); //this is line 33

   }

   /**
    * splitURL gets url from browser and processes against the conroller classes and their methods
    * @return array
    */
   private function splitURL(){
       
    //check if the the GET is set otherwise set the url to defualt class home
    $url = isset($_GET['url']) ? $_GET['url'] :"home";

    // return explode("/",filter_var(trim($_GET['url'],"/"), FILTER_SANITIZE_URL));
    return explode("/",filter_var(trim($url,"/"), FILTER_SANITIZE_URL));

   }



  }

?>

The Database class's read function is as follows. This method isn't directly referred in the error message

    public function read($query, $data = []){
        
        $stmt = self::$conn->prepare($query);
        $result = $stmt->execute($data);
     
        if($result){
            $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
            if(is_array($data) && count($data) > 0){
             
                return $data;
            }
        }
        return false;

    } 

As I mentioned earlier, this error fires on a live server but the website runs smooth in dev environment with PHP 7.4, Apache2, MySQL 8 Windows 10.

Your help is match appreciated in advance.

Abun
  • 19
  • 5
  • 4
    It means that `$this->loadModel("User")` is returning a boolean, what does this code do (you don't show the source for `loadModel()`) – Nigel Ren Oct 05 '21 at 06:16
  • 1
    Does this answer your question? [What means Call to a member function on boolean and how to fix](https://stackoverflow.com/questions/31813722/what-means-call-to-a-member-function-on-boolean-and-how-to-fix) – Michel Oct 05 '21 at 06:45
  • And if it only happens on the live server, check your paths. – Michel Oct 05 '21 at 06:46
  • @NigelRen, you are right, I didn't include the loadModel($model) method. Here is the code for the model `protected function loadModel($model){ if(file_exists("../app/models/". strtolower($model) . ".class.php")){ include "../app/models/".$model.".class.php"; return $model = new $model(); }else{ return false; } } }` @Michel the link helped in exploring the issue, however, in my case, the views load, products are retrieved from the database, and everything works fine until I attempt to sign in , signup, or check for login status. – Abun Oct 05 '21 at 07:23

1 Answers1

0

I learned this the hard way and I hope this can help someone with similar issues. The cause of the error because of how windows and Linux deals with case sensitivity in file names. In Windows, file names aren't case sensitive while in Linux - they are. So, that was the reason why the website was running smooth in the local dev environment(Windows machine) but throwing an error on a live server(which is Linux). To see the difference, refer to my earlier comment in this thread.

protected function loadModel($model){
  if(file_exists("../app/models/". strtolower($model) . ".class.php")){
      include "../app/models/".strtolower($model).".class.php";
            
      return $model = new $model();

    }else{
      return false;
    }
   }
}

In the "include" line, you can see that I added the strtolower function to include the proper model and that solved the issue.

Abun
  • 19
  • 5