Home  >  Article  >  Backend Development  >  Remember a mistake made because of shared variables

Remember a mistake made because of shared variables

藏色散人
藏色散人forward
2020-10-16 14:01:281706browse

Below by # column for everyone to remember the mistakes of sharing variables once, I hope to help friends who need it!

Remember a mistake made because of shared variables

The problem occurs

In models/User. php

var UserModel = new (User)

Controller

    if models.UserModel.Token == "" {
        models.UserModel.Token = "hello world"
    }

Becausevar UserModel = new (User) will onlynew Once, this variable is shared with each request.

As a result, this if will only be executed once in the future, or it can only obediently new a new pointer.

obedientnew

    var UserModel = new (models.User)
    if UserModel.Token == "" {
        UserModel.Token = "hello world"
    }

Ending

It took several hoursdebug to find the problem, and I made a basic mistake.                                                                                                               

The above is the detailed content of Remember a mistake made because of shared variables. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete