Synchronize in php or zend framework

守給你的承諾、 提交于 2019-12-12 02:53:57

问题


Is PHP has synchronize mechanism like Java? In my project different users can continue with same transaction.

As example, I send a mail with payment details page URL to different user. So they can view that page at same time. Also they can continue to same transaction. I want deny (Show message) that page for users when one user already view the page. In java we can use synchronize for object. How can i do it in php or zend framework?


回答1:


PHP is NOT like Java.

Each request run in a separate stack with separate variables. You can share objects in PHP only using extension like memcache etc.

My advise is to use the locking mechanism of the file system. For example:

<?php
$fp = fopen( $filename,"w"); // open it for WRITING ("w")
if (flock($fp, LOCK_EX)) {
    // do your stuf here
    flock($fp, LOCK_UN); // unlock the file
} else {
    // flock() returned false, no lock obtained
    print "Could not lock $filename!\n";
}
?> 



回答2:


I'm struggling to understand what you're trying to achieve here - either in PHP or Java. It seems you want to apply a mutex to a dataset - however that's not a side effect of synchronized - not it's purpose. It shoudn't be necessary if you use the session id and a FSM to control access.



来源:https://stackoverflow.com/questions/9191317/synchronize-in-php-or-zend-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!