-1

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

tereško
  • 58,060
  • 25
  • 98
  • 150
  • Can you show us the Mysql.php code – Jonnix Oct 17 '11 at 10:59
  • Can you post the code that constructs the UserManagement object and calls `validate_User()`? – bcmcfc Oct 17 '11 at 11:01
  • I can't see anything obviously wrong there, but it implies `$this->mysql` hasn't been set - or maybe it got set and then overwritten by code elsewhere in your class. What's in the `Mysql.php` file? Is there anywhere else that `$this->mysql` gets overwritten? – Daren Chandisingh Oct 17 '11 at 11:01
  • Do you not have a separate script that does something like: `$um = new userManagement(); $um->validate_User($user, $pw);` ? – bcmcfc Oct 17 '11 at 11:13
  • 1
    I think this is all about debugging. Check if after `$this->mysql = new Mysql();` there is object. If so - examine what is done between constructor and validate_User, because something could overwrite variable. Anyways, this would be much more productive than our guessings here. – dmitry Oct 17 '11 at 11:17
  • I have added the other code via pastebin that was requested. – project.crazy Oct 17 '11 at 11:18

1 Answers1

0

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.

bcmcfc
  • 25,966
  • 29
  • 109
  • 181
  • The user management class was constructed but thinking about what you said and going through the UserManagement class again. manage_Session uses validate_User and the variable was empty because it was trying to create it after calling manage_Session(). A few hours of pulling out my hair for placing a line of code in the wrong place. Thanks you very much for your help guys sorry it was such a silly mistake. – project.crazy Oct 17 '11 at 11:25