1

I am getting a Fatal Error while calling a public method using Scope Resolution Operator. Here is the part of the code:

Class userrole {
    public function get_premium_subscritpion($userID) {
        $userID = ($userID == '') ? $this->user_info->ID : $userID;
        if (empty($userID)) return false;

        /* Check if user has subscribed */
        if ($this->get_subscription($userID) && $userID != '') {
            return true;
        }
    }
}

$role = userrole::get_premium_subscritpion(1);
2
  • 3
    in order to call the method such you must declare mehtod as a public static function Commented Jun 16, 2015 at 6:53
  • 2
    You can't call $this if you call the method in the static way. You must change $this-> to self::get_subscription (if get_subscription is also static). Otherwise you need to create an instance of userrole in the method or before you call get_premium_subscription. Commented Jun 16, 2015 at 6:56

1 Answer 1

4
$role = userrole::get_premium_subscritpion(1);

I notice that you are trying to call a non-static function in a static way

You may either change the function to static or change the way you call this function

public static function get_premium_subscritpion {

or

$obj = new userrole();
$role = $obj->get_premium_subscritpion(1);
Sign up to request clarification or add additional context in comments.

4 Comments

The first change would still lead to the same error as long as $this->get_subscription would not also get changed into a static scope. Also assuming that this method is even inside the real userrole class.
i changed the public method to static but it still not worked. It still return the same error.
if you change it to static you may not use $this inside the function. Use self:: instead. But you could not do that in this case because your whole code is in a non-static context. Just try the second way I wrote. Thanks
Thanks for the help Eason. The second option works fine for me!!

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.