Search code examples
phpphp-7.1

$this issue in php 7.1.9


I have a class which I am using $this with and since upgrading from 5.6 to 7.1 I cannot get to work. I'm confused as to why? Please see code sample below:

class user_BL extends BLL {

public function getCurrentUserFromSession($userid) {
            $userrecord = array();

            $query = new query();
            $query->addCriteria("userid", $userid , "=");

            $userrecords = $this->getDataByQueryObj($query, new user_DAL());

            if (isset($userrecords[0])){
                $userrecord = $userrecords[0];
                $lastlogindetails = $this->getLastLoginDetailsAsArray();
                $userrecord['logindatetime'] = astlogindetails['logindatetime']; 
            }

            return $userrecord;
        }
}

getLastLoginDetailsAsArray is a function on the BLL base class. The IDE interprets this ok and sees that its available to be used.

Anyhelp with this would be really helpful.

Thanks,

Deano

EDIT: the error I'm getting is Fatal error: Uncaught Error: Using $this when not in object context

Forgot to add that it was a long day yesterday. :)


Solution

  • After digging into the error I've found the call was being made like this:

    user_BL::getCurrentUserFromSession();
    

    which seems to be the problem. Calling it like this:

    $userbl = new user_BL();
    $userbl->getCurrentUserFromSession();
    

    Corrects this. Thanks for everyones help.