Using $this when not in object context

I have upgraded hosting from shared to VPS. I am using php 7.0 now. After migrating i am getting following error in my rest login API. I don’t have this issue in Website but Rest API giving me this error.

Error :




    "name": "Exception",

    "message": "Using $this when not in object context",

    "code": 0,

    "type": "Error",

    "file": "/var/www/xyz.com/common/models/User.php",

    "line": 179,

Login Function




 public function actionLogin(){

       $request = \Yii::$app->request;

       if(empty($request->post('device_token'))){

            throw new \yii\web\HttpException(422, 'Please provide device_token.'); 

        }

       if(empty($request->post('username')) || empty($request->post('password')))

            throw new \yii\web\HttpException(403, 'Your credential is not valid');

       

           $user = \common\models\User::findOne(['mobile' => $request->post('username'), 'status' => 'Active']); 

        

         

        if(empty($user))

            throw new \yii\web\HttpException(403, 'Incorrect username or password');

            $this->password_hash = $user->password_hash;

            $isPass = \common\models\User::validatePassword($request->post('password'));

        if(!$isPass)

             throw new \yii\web\HttpException(403, 'Incorrect username or password');

        

        $dt = \backend\models\AppUser::findOne(['user_id'=>$user->id,'device_token'=>$request->post('device_token')]);

        if(!$dt){

        $checkToken = \backend\models\AppUser::findOne(['device_token' =>$request->post('device_token')]);

        if($checkToken){

            $deleteToken = \backend\models\AppUser::deleteAll(['device_token' =>$request->post('device_token')]);

        }

        $dt = new \backend\models\AppUser();

        }

        $dt->user_id = $user->id;

        $dt->device_token = $request->post('device_token');

        $dt->save(false);

        $data['user'] = $user;

        

        $profileCheck = \backend\models\UserProfile::findOne(['user_id'=>$user->id]);

        

        if($profileCheck){

            $data['profilecheck'] = array('profile'=>'yes');

        

        }else{

            $data['profilecheck'] = array('profile'=>'no');

            

        }

        $data['notiCount'] = \backend\models\Notification::find()->where(['sendto'=> $user->id, 'status'=>'Pending'])->count();

        $data['count'] = \backend\models\Cart::find()->andWhere(['user_id'=>$user->id])->count();

        return $data;

    }



I have this code in User Model:




/**

     * Validates password

     *

     * @param string $password password to validate

     * @return boolean if password provided is valid for current user

     */

    public function validatePassword($password)

    {

        return Yii::$app->security->validatePassword($password, $this->password_hash); //Line Number 179

    }



Please do let me know that how to solve it. Thanks.

This line tries to call a static function, but User::validatePassword is not static but requires an instance of User.

to add to what @softark has pointed out you need to create an instance of user class and call validatePassword on that, here your code modified


$isPass = (new \common\models\User)->validatePassword($request->post('password'));