Why doesn't php have application variables like asp and asp.net?
-
This would make possible massive improvements in performance for php applications and it was an old idea 10 years ago. edit: for php programmers who don't know what I mean, application variables are similar in functionality and usage to session variables in that they persist between requests, except they apply across the whole application scope (ie all sessions and server processes for that web application / site). The closest approximation to this functionality in php appears to be memcached which enables persistent storage of frequently used data in memory, however this is IMHO inferior to application variables since data is set and retrieved via a (relatively slow) network request where as application variables are "passed by reference" http://www.w3schools.com/asp/asp_applications.asp
-
Answer:
PHP was built on top of the "Share Nothing" architecture, meaning that running one PHP process does not affect other PHP processes in terms of memory and environment. While I'm not sure what was the reasons for developing PHP this way back then, there is no doubt that it had a major impact on PHP's rapid success in among web developers, in times when dynamic website were considered top technology: It allowed PHP to be integrated onto various web servers and technologies, commercial and open-source. It simplifies PHP coding and web-dev for beginners, low-scale projects and startups. Developers don't need to think of load multiple sessions, but rather develop as if the system is to serve one user a time. It allows distribution of PHP apps accross multiple nodes, simply and instantly, without the need to consider shared resources. I'm sure there are lots more advantages, as there are disadvantages, such as resource expensiveness, performance impact and of course, it's harder to share resources between sessions and processes. There are ways to accomplish resource sharing, such as memcache, files and other databases, but this is another topic. Edit: About memcache slow access, it's true if you plan to run your app on a single node. However, once applications expand and scale, you must find a way to share in-memory resources between processes running on different nodes. In this situation, the network cost is inevitable, whatever technology you use. Memcache is a great solution (other noSQL solutions are great also) because the network latency is relatively small and the data is stored in RAM.
Oren Yagev at Quora Visit the source
Other answers
There are a few ways this could be done. You could use APC[1] to store and fetch custom values in memory. If you just want some variables (say environment/config variables) to be shared across requests, you could also plonk them into your PHP .ini files or Apache conf files. You could check out ini_set(), ini_get() and get_cfg_var() to get/set values from .ini files. However, in each of these cases, I'm not sure if PHP provides a locking mechanism. [1] - http://php.net/manual/en/book.apc.php
Rohit Nair
If you mean session variables, those are available since PHP 4: http://www.php.net/sessions
Manuel Lemos
Related Q & A:
- Why doesn't ClickOnce in Visual Studio deploy content files from dependent assemblies?Best solution by Stack Overflow
- Why doesn't my PHP function work as expected?Best solution by Stack Overflow
- Why doesn't MySQL upload my data properly?Best solution by php-mysql-tutorial.com
- Why doesn't the messenger load?Best solution by Yahoo! Answers
- Why doesn't my computer alert me of new e-mail?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.