473,320 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

PHP static scope

ks
Hello,
I'm kinda new to PHP and I'm running into something that just seems
odd. There's a good chance I'm just doing something wrong though. I'm
trying to implement a simple singleton:

private static $instance;
public static function Instance()
{
if (!isset(self::$instance))
{
self::$instance = new MyClass();
}
return self::$instance;
}

which pretty much follows the common pattern. I've noticed through,
either by using a var_dump on self::$instance or by simply outputting
"test" inside the IF statement, that my if statement always executes.
If I call Instance twice within the same request, the singleton works,
but if I refresh or open a new browser, each request seems to create
it's own version. Am I doing something wrong or are static's scoped to
thread requests? I would expect the block within the if to fire once
throughout the life of the application (or until php resets itself or
whatever).

I'm just messing with things on IIS - incase that could be the issue.

Jan 23 '06 #1
2 1759

ks wrote:
Hello,
I'm kinda new to PHP and I'm running into something that just seems
odd. There's a good chance I'm just doing something wrong though. I'm
trying to implement a simple singleton:

private static $instance;
public static function Instance()
{
if (!isset(self::$instance))
{
self::$instance = new MyClass();
}
return self::$instance;
}

which pretty much follows the common pattern. I've noticed through,
either by using a var_dump on self::$instance or by simply outputting
"test" inside the IF statement, that my if statement always executes.
If I call Instance twice within the same request, the singleton works,
but if I refresh or open a new browser, each request seems to create
it's own version. Am I doing something wrong or are static's scoped to
thread requests? I would expect the block within the if to fire once
throughout the life of the application (or until php resets itself or
whatever).

I'm just messing with things on IIS - incase that could be the issue.


You're misunderstanding how this is supposed to work. Forget, for a
moment, about sessions, cookies, get and post. So, when the user
requests a page, PHP fires up, runs the PHP code and outputs HTML to
the browser. That script is done. No more. Not coming back.
Anything done on that page is "forgotten" before the next request.

Put another way, each request is independent. So, your observation
that "If I call Instance twice within the same request, the singleton
works" is correct, and is the expected behavior.

If you want things to persist between requests (or be shared among many
requests) you'll need to save that state somehow. This is the intended
use of sessions, cookies, get and post. If you want to share something
among many requests then you're looking at something like a database or
shared memory.

Jan 23 '06 #2
ks
Thanks,
I'm coming from an ASP.NET background where the use of static is more
traditional (or atleast, independent of web paradigms). In ASP.NET it
can obviously lead to considerably thread-safety issues if improperly
used.

I'll simply design my solution differently :)

Thanks for the help,
Karl

ZeldorBlat wrote:
You're misunderstanding how this is supposed to work. Forget, for a
moment, about sessions, cookies, get and post. So, when the user
requests a page, PHP fires up, runs the PHP code and outputs HTML to
the browser. That script is done. No more. Not coming back.
Anything done on that page is "forgotten" before the next request.

Put another way, each request is independent. So, your observation
that "If I call Instance twice within the same request, the singleton
works" is correct, and is the expected behavior.

If you want things to persist between requests (or be shared among many
requests) you'll need to save that state somehow. This is the intended
use of sessions, cookies, get and post. If you want to share something
among many requests then you're looking at something like a database or
shared memory.


Jan 23 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: A | last post by:
Hi, Consider this code: //Header File - Foo.h int i = 0; // non-static global variable class Foo{ ...
6
by: pembed2003 | last post by:
Hi all, I am reading the book "C++ How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
2
by: dover | last post by:
If a class defined as classA, what's the meaning of "static classA objectA;", within a file scope or a function scope or a class member function scope? Many thanks!
8
by: Ganesh Kundapur | last post by:
Hi all, struct test { static int x; }X; main() { X.x = 100; }
12
by: codefixer | last post by:
Hello: I am trying to understand the use of static in this program. http://nanocrew.net/sw/nscdec.c for "inverse" matrix. What difference would it make if it were not static and just "const...
4
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
6
by: vim | last post by:
hello guys plz tel me the differances between global static and local static. If possible with examples
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
27
by: arkmancn | last post by:
Any comments? thanks. Jim
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.