473,788 Members | 2,848 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

php 5: Static class variables

Hi folks!

I noticed in my static example which I did in PHP that the static
variable is not stored over multiple php pages. So this does differ
than to other OO languages like JSP. Is this so or did I something
wrong?

MyClass.php:
<?
class MyClass
{
static $MyVar = 123;
function getMyVar()
{ return MyClass::$MyVar ; }
}
?>

1. Call on my server
MyPage1.php
<?
require("MyClas s.php");
// set here the value
MyClass::$MyVar = 456;

echo MyClass::$MyVar ; // prints 456

myInst1 = new MyClass();
echo myInst1->getMyVar(); // prints 456

?>

2. Call on my server
MyPage2.php
<?
require("MyClas s.php");
myInst2 = new MyClass();
echo myInst2->getMyVar(); // prints 123 ??? Expected was 456

?>

Thanks
Mark Egloff

Oct 14 '05 #1
2 24004

joes wrote:
I noticed in my static example which I did in PHP that the static
variable is not stored over multiple php pages. So this does differ
than to other OO languages like JSP.
Ahem ... JSP is not directly an object oriented language. Java, on
which JSP is founded, is object oriented, though.
Is this so or did I something
wrong?


It is indeed the case that static variables behave differently in PHP
compared to Java. In the case of Java Servlets and/or JSP all the HTTP
requests are (generally) served by one Java Virtual Machine. Thus,
static variables of classes just exist once on a server and do not have
any notion of HTTP requests.

In PHP though, the PHP environment is newly created for each HTTP
request. Without further measures, the PHP scripts are even recompiled
for each request. So, static class variables are deleted after a
request has been completed.

You should note that in Java static variables are not only common to
the requests of a single session, but to all requests that arrive on
the server. Thus, you should think twice (better even more) before
using static variables in a JSP or Servlet environment. The most common
use case for static variables in these contexts is resource sharing
between the requests. One example for that is the Struts configuration.

Bye

Nils

Oct 14 '05 #2
Thanks

I know the java environment very well. acting as at teacher and sun
certified architect, but I am very new on PHP.

regards
Mark

Oct 15 '05 #3

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

Similar topics

0
1216
by: The Dark Seraph | last post by:
I have a singleton object that works perfectly from inside my main .py file. However, when I want to access the object that backs the singleton from another file ( either from an execfile() or thru my telnet listener ), it appears the underlying object is reset to null, resulting in a duplicate object being created. I figure I am doing something painfully stupid, but for the life of me, I can't see it. Here is my singleton wrapper:
11
2559
by: Neil Zanella | last post by:
Hello, When an "std::map<int, int> foobar;" statement appears in a C++ program followed by a statement of the form "foobar;" where nothing has ever been inserted at position 123 into map foobar, the C++ standard states that a new element is created at position 123 and is initialized to zero. That is, in this case, the pair (123, 0) is inserted into the map. I am interested in knowing what the standard says should happen in the
3
4144
by: MuZZy | last post by:
Hi, Consider i have a class: class CTest { public static int Counter = 0; public CTest() { <...> Counter ++;
5
3610
by: mast2as | last post by:
Hi guys Here's the class I try to compile (see below). By itself when I have a test.cc file for example that creates an object which is an instance of the class SpectralProfile, it compiles fine. 1 / Now If I move the getSpectrumAtDistance( const T &dist ) method definition in SpectralProfofile.cc let's say the compiler says core/profile.cc:199: error: `kNumBins' was not declared in this scope
55
6249
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in C# in some way? Or maybe no, because it is similar to a global variable (with its scope restricted) which C# is dead against? Zytan
8
2889
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
2
3625
by: HerbD | last post by:
I have a loooong debugging session behind me! I finally found the reason for the problem and now would like to know, if it is a bug in my code or not standardconformant behavour of the compiler(s) or not a bug at all and just normal behavior: This simple sample program illustrates the problem. Please use separate header (.h) and code (.cpp) files, because it plays a role in the problem... /************************************** * Module: ...
2
2485
by: Ranganath | last post by:
Hi, Why is there a restriction that only integral types can be made static constant members of a class? For e.g., class B { private: static const double K = 10; };
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.