'PHP 8.1.3 Why does a method call fail to start when called?
I am trying to create an MVC template for my site and am working on testing authentication code. No errors show up on the screen or log files, however, the code does not complete. It simply dies when I try calling findUser().
When I call from my Authentication controller to the associated class/method, nothing happens. Here is the code for the area. Note the echo statements, and how I never get "Begin: tAuthentication - findUser method"
I am using PHP 8.1.3_1 and the latest version of Apache.
# File: InitializeMVC.php
require_once CORE_PATH . 'Database.php';
# File: AuthenticationControl.php
require_once MODEL_PATH . 'tAuthentication.php';
class Authentication {
public object $AuthenticationModel;
public function __construct(){
echo "Begin: AuthenticationControl | construct: " . " <br />";
$AuthenticationModel = new tAuthentication;
}
public function login() {
echo "Begin: AuthenticationControl | login: " . " <br />";
$data['Uname'] = "testuser";
echo "Data loaded. Go to model.<br />";
$userid = $AuthenticationModel->findUser($data['Uname']);
echo "Return to AuthenticationControl with userid. <br />";
}
}
# File: tAuthentication.php
class tAuthentication extends Database {
public function __construct() {
echo "Begin: AuthenticationModel | construct: " . " <br />";
$this->db = new Database;
public function findUser($username){
echo "Begin: tAuthentication - findUser method <br />";
$this->db->query('SELECT PeopleID FROM vAuthenticate WHERE username = :username OR email = :username');
$this->db->bind(':username', $username);
$row = $this->db->GetData();
}
}
# Begin: AuthenticationControl | construct:
# Begin: tAuthentication | construct:
# Begin: AuthenticationControl | login:
# Data loaded. Go to model.
Solution 1:[1]
You forgot to use
$thisinAuthentication::__construct()andAuthentication::login()as well. If you want to refer to an object's property from within its own method you need to use$this. Otherwise it refers to a local variable within the method if it exists or to an undefined variable. The latter also generates a notice except if this is an assignment to the formerly undefined variable.So in these methods you need to write
$this->AuthenticationModelinstead of$AuthenticationModel.In the
Authentication::login()method you are also referring to an array's key without defining the variable itself as an array:$data['Uname'] = "testuser";...$datashould be defined as an array like$data = [];or$data = array();before accessing a key of it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
