douzao2992 2014-02-03 20:48
浏览 34

php:当从文件中读取具有不同静态变量值的相同类对象时,什么是静态类变量的行为?

Here's the idea: I'm building a file upload site in php where the records of users are stored in a flat file as an array of objects of class User. In the structure of the User class i have defined $iUsers as a static variable so as to count the number of users, without the need to have a separate records_number file.

The experiment: Suppose some first visitor Joe registers, logs in and does stuff. His version of $iUsers is 1 since he is the only user. Meanwhile other visitors register as well. So after Joe has uploaded a file, an update_user_activity function is triggered by ajax, which will attempt to read the records_file and update it.

My question: What happens when the objects' array is read from the file? Does User::$iUsers remains unchanged, does it gets the file's class static variable (for example 6, if 5 more visitors have registered), or gains some obscure, undefined value?

EDIT: here is a sample of the class implementation as requested:

class User {
// user details, username, password, e-mail
static private $iUsers=0;
public function __construct($usr, $pwd, $eml) {
    //initializes user details
    $iUsers++;
}

// reads records from records file and locks the records file
static private function read_records(&$arr) {
    //lock file?
    //if User::check_connected()(checks session var) update $arr for the current user
    //open REC_FILE
    //unserialize to $arr
    //close REC_FILE
}
// Updates records from records file to the given array
static private function update_records(&$arr) {
    //unlock file
    //open REC_FILE
    //serialize $arr to file
    //close REC_FILE
}

static private function unlock_records() {
    //unlock file
}

// update function
private function update_user_activity() {
    read_records($recs);
    foreach($recs as $cur) {
        if($cur->sUsername === $this->sUsername) {
            //stuff here (updates the $cur
            update_records($recs);
            return true;
        }
    }
    unlock_records();
    return false;
}
static public function register(&$usr, &$pwd, &$eml) {
    //$recs array of records
    read_records($recs);
    if(iUsers<MAX_USERS) {
        // loop through the records --- unlocks_records and returns error if same name or e-mail is found
        $newuser = new User($usr, $pwd, $eml);
        array_push($recs, $newuser);
        update_records($recs);
        return "success";
    }
    else {
        unlock_records();
        return "No capacity for new users!";
    }
}

// login function
static public function login(&$usr, &$pw) {
    //$recs array of records
    read_records($recs);
    if(User::$iUsers>0) {
        foreach($recs as $cur) {
            if($cur->check_matching($usr, $pw)) {
                if($cur->$bActivated) {
                    // logged in
                    // stuff
                    update_records($recs);
                    return $cur;   //return whatever is going to be stored in _SESSION array;
                }
                else {
                    unlock_records();
                    return "Your account is deactivated!";
                }
            }
        }
        unlock_records();
        return "The username or password provided is wrong.";
    }
    else {
        unlock_records();
        return "There are no users registered!"; // no users registered
    }
}

}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP