开发者

Will a php Singleton class instance be retained across multiple sessions?

For the sake of a simple example, if I want to count the number of hits a script gets without the use of disk storage, can I do this with a static class member?

User1:

<?php
$test = Example::singleton();
$test->visits++;
?>

User2:

<?php
$test = Example::singleton();
开发者_JAVA技巧$test->visits++;
?>

Will the value of $visits be 1 or 2?


No. Each request will spawn a new process. Nothing survives between them.

You can retain state using sessions, which are essentially a disk-based serialization mechanism. Sessions them selves rely on cookies to identify the data between requests (But the data it self is stored in a file on the server). As such, they are local to the user and not suitable for your needs. The standard way to store that kind of data in a PHP application would be in a database.


$visits will be 1 in both case.

Singletons are per request and not per machine / host. Each request will have its own instance.

I don't think you can count the number of hits for one page without some kind of disk storage / database.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜