0

I have a login.php and authenticate.php

I want to access a variable inside the authenticate.php inside a class. I want to get the error message from authenticate class to my login.php

This is my class inside Authenticate.php

Class Authenticate {

    public $invalidUserErrMsg = "sdfs";

    static public function LDAPAuthenticate() {
                 //connection stuff
            } else {
                $msg = "Invalid username / password";
                $this->invalidUserErrMsg = $msg;

            }
    }

    static public function invalidUserErr() {
        echo $hits->invalidUserErrMsg;
        return $this->invalidUserErrMsg;
    }

}

This is how I'm printing inside login.php

<?php 
    $error = new Authenticate();
    $error->invalidUserErr(); 
?>
4
  • You're returning the value from invalidUserErr() so just assign that when you call it in your login Commented Jan 12, 2016 at 9:59
  • use $error = new Authenticate::invalidUserErr(); Commented Jan 12, 2016 at 10:01
  • you get an error because there is not if() within the LDAPAuthenticate() Commented Jan 12, 2016 at 10:14
  • @Lars There's a condition inside that function, and the message si returning within the class, I just want to position the error message properly cause its showing on the top of the page. Commented Jan 12, 2016 at 10:15

2 Answers 2

3
Class Authenticate {

    public $invalidUserErrMsg = "sdfs";

    public function LDAPAuthenticate() {
        if($hello) {
             echo 'hello';
        } else {
            $msg = "Invalid username / password";
            $this->invalidUserErrMsg = $msg;

        }
}

   public function invalidUserErr() {
        return $this->invalidUserErrMsg;
    }

}

<?php 
    $error = new Authenticate();
    echo $error->invalidUserErr(); 
?>

Don't echo the variable within the class but echo the method on login.php. There is no need to make it a static function if you are going to instantiate the object anyway.

Check out this page on the static keyword

Sign up to request clarification or add additional context in comments.

8 Comments

Fatal error: Uncaught Error: Call to undefined method Authenticate::invalidUserErr()
That is because you declared the function as static
Just tested the code 'as is' in my answer and it works
didn't see the echo part! thnaks!
No worries, Good luck ;)
|
0

For accessing static function you need

<?php 
  $error = new Authenticate::invalidUserErr();

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.