PHP OO Call to a non-object [duplicate]
I have recently started coding in php again and am learning Object Orientated PHP. I have come across an issue that I cannot seem to get my head round with an error:
Fatal error: Call to a member function verify_Username_And_Password() on a non-object in C:\wamp\www\sso\lib\UserManagement.php on line 57
I understand that the program seems to think that the mysql class variable is empty but not why when it is instanced.
The function verify_Username_And_Password ($username, $password) is defined in the class Mysql.
The following is code from UserManagement.php with parts taken out that are not needed.
<?PHP
require_once 'lib/Mysql.php';
class UserManagement {
private $mysql;
function __construct(){
session_start();
$this->manage_Session();
$this->mysql = new Mysql();
}
function validate_User ($username, $password){
$user_Check = $this->mysql->verify_Username_And_Password($username, md5($password));
if($user_Check) {
return true;
} else {
return false;
}
}
}
I must be missing something basic about OO in PHP. I would be grateful for any advice anyone can give. I will be happy to post more code if needed.
As requested here is: Mysql.php pastebin. UserManagement.php pastebin
Without seeing the code that does the work, my guess would be you are using the validate_User method without first constructing a UserMangement object.
$um = new UserManagement();
$um->validate_User($username, $password);
If you wish to call validate_User without first constructing a UserManagement object it needs to be a static method. Without the UserManagement object being constructed Mysql is never constructed and saved in the attribute inside your class.
精彩评论