Ayush
Ayush

Reputation: 42450

undefined function when function is present

I have a PHP class that looks like this:

class userAuthentication
{
    public static function Authenticate()
    {
        if (isset($_COOKIE['email']))
        {
            verify($someVar, getPass($_COOKIE['email']);
        }
    }

    public static function getPass($email)
    {
    }

    public static function verify()
    {
    }
}

At times (I can't pin-point exactly when), I get a fatal error :

Call to undefined function getPass()

The error is thrown at the line where I call the function in the above code sample. Why is this happening when the function clearly exists.

Upvotes: 1

Views: 106

Answers (4)

h00ligan
h00ligan

Reputation: 1481

You're not calling it as a static function.

From within the class use either:

self::getPass($email);

or (for late static binding):

static::getPass($email);

and from outside the class:

userAuthentication::getPass($email);

The line should probably be:

self::verify($someVar, self::getPass($_COOKIE['email']);

Upvotes: 1

Hubro
Hubro

Reputation: 59378

I would assume the error occurs when you try to run getPass($_COOKIE... since you're calling it wrong. Since the function is a class method, you have to run it like this:

$this->getPass(...);

or if you're calling it statically:

self::getPass(...);

Making your code:

class userAuthentication
{
    public static function Authenticate()
    {
        if (isset($_COOKIE['email']))
        {
            self::verify($someVar, self::getPass($_COOKIE['email']);

            // Or...

            $this->verify($someVar, $this->getPass($_COOKIE['email']);
        }
    }

    public static function getPass($email)
    {
    }

    public static function verify()
    {
    }
}

Upvotes: 1

phihag
phihag

Reputation: 288260

verify is not a global function, but only valid in the scope of your class. You want

self::verify($someVar, getPass($_COOKIE['email']);

Upvotes: 3

Maerlyn
Maerlyn

Reputation: 34105

It's a static function in a class. Use self::getPass() or static::getPass() if you want to take advantage of Late Static Binding. Same goes for verify().

Upvotes: 5

Related Questions