I've just started PHP developement and I would like to know a bit more about variables scope.
I know about the scope inside the script. I just want to know, how variables are treated between different users. Let's say I have a class :
class SomeClass {
public static $myVar = 0;
public static function doSomethingToMyVar(){
// $myVar gets manipulated
}
}
NOTE: I know it's an awful design, so it's just for showing an example of my question.
Now my question is: if user A uses the page so that doSomethingToMyVar is called, does user B get modified value of $myVar? I mean, do different users share static variables or PHP gives seperate sets of variables for each user?
Related
I am looking for assurance that static variables are NOT stored between PHP requests. The following previous questions:
PHP static variables across multiple .php pages
Does static variables in php persist across the requests?
Static variables across sessions
clearly say that they aren't but they are more in the context of providing a way to maintain state rather than a specific discussion of what is the expected behaviour.
As an example, if I have PHP code as follows:
function myfunc()
{
static $a=0;
print $a++;
}
for ($i=0;$i<10;$i++) myfunc();
then I get output of 0123456789 every time I run it. My instinct/understanding of PHP makes me fairly sure that this has to be the case.
In my own experiments, I've shut a (preforking) apache down to one child to make sure that the variable isn't remembered between requests. It isn't remembered between requests as I'd expect. But this is only one scenario in which PHP runs.
What I'm looking for is:
A link to an official piece of documentation that says this is the expected behaviour and won't change. The relevant piece of PHP documentation doesn't mention this explicitly (except in the comments).
Or, an example of when static variables are remembered across requests such as webservers or performance enhancing PHP frameworks out there which will potentially not clear static variables to increase speed between requests.
PHP does not preserve the application state between requests. During an PHP applications life cycle the application is freshly executed with each request. Static variables are meant to preserve the value of a variable in a local function scope when execution leaves the scope. Nowhere in the documentation does it mention that static variables are meant to preserve value across requests.
Yes, you are right, static variables or any variable in PHP except $_SESSION lives only through one request. But you can do it using $_SESSION;
class MyClass
{
public static $a = 0;
public static init()
{
self::$a = isset($_SESSION['a']) ? $_SESSION['a'] : 0;
}
public static printA()
{
self::increaseA();
print(self::$a);
}
public static increaseA()
{
self::$a++;
$_SESSION['a'] = self::$a;
}
}
myClass::init();
for ($i=0;$i<10;$i++) myClass::printA();
The particularity of PHP is that every request reload the whole PHP Code. So, A static method/property get it's default value at each new request.
A confirmation of the fact that the "whole php code is reloaded at every request" is that you can find persistant method like for your database access, in order to avoid making a new connection to your DB for each request (see: mysql_pconnect)
I am learning MVC based web pages in php.I have few php files with multiple classes & methods in MVC layers.I have many variables & large arrays that need to be accessed in several files.some sample given below:
$label = array("lng"=>Language",tlabel"=>"Title",
"content"=>"Details", "tm"=>"Date",
"authr"=>"Post By");
$route_url;
$database;
$navbar_text;
$footer_text;
I saw few php example about using php keyword global. But using it in many parts of the code will become
very messy to keep track.I also saw another technique where every common things goes inside a special class
called Config like given below:
// does it need auto load? i don't know
class Config
{
public static $label = array("lng"=>Language","tlabel"=>"Title",
"content"=>"Details", "tm"=>"Date",
"authr"=>"Post By");
public static $route_url;
public static $database;
public static $navbar_text;
public static $footer_text;
}
echo SmtpConfig::$label["tlabel"];
So, please suggest the correct way in which MVC web developer do such things.Thanks
You can use Static Class Variables.
The second approach to use $GLOBAL vars.
But it is not a good approach to use a lot of global variables.
You can do that using $GLOBALS super global array which is accessible throughout whole PHP script.
Add your variables in $GLOBALS super global array like,
$GLOBALS['data'] = 'Something';
and now you can access this variable globally like,
$something = $GLOBALS['data']
I am looking for assurance that static variables are NOT stored between PHP requests. The following previous questions:
PHP static variables across multiple .php pages
Does static variables in php persist across the requests?
Static variables across sessions
clearly say that they aren't but they are more in the context of providing a way to maintain state rather than a specific discussion of what is the expected behaviour.
As an example, if I have PHP code as follows:
function myfunc()
{
static $a=0;
print $a++;
}
for ($i=0;$i<10;$i++) myfunc();
then I get output of 0123456789 every time I run it. My instinct/understanding of PHP makes me fairly sure that this has to be the case.
In my own experiments, I've shut a (preforking) apache down to one child to make sure that the variable isn't remembered between requests. It isn't remembered between requests as I'd expect. But this is only one scenario in which PHP runs.
What I'm looking for is:
A link to an official piece of documentation that says this is the expected behaviour and won't change. The relevant piece of PHP documentation doesn't mention this explicitly (except in the comments).
Or, an example of when static variables are remembered across requests such as webservers or performance enhancing PHP frameworks out there which will potentially not clear static variables to increase speed between requests.
PHP does not preserve the application state between requests. During an PHP applications life cycle the application is freshly executed with each request. Static variables are meant to preserve the value of a variable in a local function scope when execution leaves the scope. Nowhere in the documentation does it mention that static variables are meant to preserve value across requests.
Yes, you are right, static variables or any variable in PHP except $_SESSION lives only through one request. But you can do it using $_SESSION;
class MyClass
{
public static $a = 0;
public static init()
{
self::$a = isset($_SESSION['a']) ? $_SESSION['a'] : 0;
}
public static printA()
{
self::increaseA();
print(self::$a);
}
public static increaseA()
{
self::$a++;
$_SESSION['a'] = self::$a;
}
}
myClass::init();
for ($i=0;$i<10;$i++) myClass::printA();
The particularity of PHP is that every request reload the whole PHP Code. So, A static method/property get it's default value at each new request.
A confirmation of the fact that the "whole php code is reloaded at every request" is that you can find persistant method like for your database access, in order to avoid making a new connection to your DB for each request (see: mysql_pconnect)
Not sure if it's global per se, but what I need is a variable that is set from within a model that is dynamically generated when the model gets called. However I need to set a variable that is accessible to multiple views being pulled in through the template to use the same variable.
Its an advertisement ID my clients sponsors have multiple ad spots per page example a 486x 60 and a 160x90 spot. But what I am trying to do is when there ID is pulled at random from the bunch I want all my ad spots to be the same sponsor.
Now I have tried going in my header.php view and defining a variable like
$adsIDvar = $this->modelname->sponsorids() and then in every view I have ad placement just using $varIDvar but it doesnt seem that any of the views inherate the variable. I have tried to find information on this but most people looking for a similar notation need hard coded variables like a site title for example.
I need something that can cross the barrier, and I'd prefer to avoid sessions/cookies as I want to avoid dealing with the whole Cookie thing in the UK as a good half of the viewers of the site are from the UK and I'd rather not have to go through the effort of saying this site uses cookies blah blah accept/decline just for this purpose. Besides, if they decline, that puts a kink in my work.
If you want to import a variable from the global scope, you need to use the global keyword. For example:
class SomeClass {
public function SomeFunction() {
global $adsIDvar; // now it is imported from the global scope
}
}
It's just my opinion but maybe a better approach would be to make a special class just for handling ad ids. I might try something like this:
class AdHelper {
public static $advertiser_id;
public static function getAdvertiserId() {
if (!isset(self::$advertiser_id)) {
self::selectAdvertiserId();
}
return self::$advertiser_id;
}
protected static function selectAdvertiserId() {
self::$advertiser_id = ....; // Implement this however you like, random or whatever
}
}
// you can call it from anywhere like:
$adsIDvar = AdHelper::getAdvertiserId();
I am looking for assurance that static variables are NOT stored between PHP requests. The following previous questions:
PHP static variables across multiple .php pages
Does static variables in php persist across the requests?
Static variables across sessions
clearly say that they aren't but they are more in the context of providing a way to maintain state rather than a specific discussion of what is the expected behaviour.
As an example, if I have PHP code as follows:
function myfunc()
{
static $a=0;
print $a++;
}
for ($i=0;$i<10;$i++) myfunc();
then I get output of 0123456789 every time I run it. My instinct/understanding of PHP makes me fairly sure that this has to be the case.
In my own experiments, I've shut a (preforking) apache down to one child to make sure that the variable isn't remembered between requests. It isn't remembered between requests as I'd expect. But this is only one scenario in which PHP runs.
What I'm looking for is:
A link to an official piece of documentation that says this is the expected behaviour and won't change. The relevant piece of PHP documentation doesn't mention this explicitly (except in the comments).
Or, an example of when static variables are remembered across requests such as webservers or performance enhancing PHP frameworks out there which will potentially not clear static variables to increase speed between requests.
PHP does not preserve the application state between requests. During an PHP applications life cycle the application is freshly executed with each request. Static variables are meant to preserve the value of a variable in a local function scope when execution leaves the scope. Nowhere in the documentation does it mention that static variables are meant to preserve value across requests.
Yes, you are right, static variables or any variable in PHP except $_SESSION lives only through one request. But you can do it using $_SESSION;
class MyClass
{
public static $a = 0;
public static init()
{
self::$a = isset($_SESSION['a']) ? $_SESSION['a'] : 0;
}
public static printA()
{
self::increaseA();
print(self::$a);
}
public static increaseA()
{
self::$a++;
$_SESSION['a'] = self::$a;
}
}
myClass::init();
for ($i=0;$i<10;$i++) myClass::printA();
The particularity of PHP is that every request reload the whole PHP Code. So, A static method/property get it's default value at each new request.
A confirmation of the fact that the "whole php code is reloaded at every request" is that you can find persistant method like for your database access, in order to avoid making a new connection to your DB for each request (see: mysql_pconnect)