php variable scope with AJAX calls
What scope to php variables have with regards to AJAX calls? So for example, a file main.php
creates an instance of a class Test
:
<html>
<head>
<?php
require_once 'test.php';
$test = new Test('some parameter');
?>
</head>
<body></body>
</html>
Later on there is a jQuery post event triggered by some event:
$(document).ready(function() {
$.post(
"class/start.php",
function(data) {
// some functioning.
}, "json");
});
Then in start.php
I need access to the class I defined in main.php
. 开发者_如何学GoE.g.:
<?php
echo json_encode($test->SomeFunction());
?>
From testing this I'm fairly sure this isn't possible. I assume the scope of $test
dies when main.php
comes from the server. So what do I need to do to access the instance of that class? Do I need to add $test
as a session variable or is there some other better way?
You could session_start()
and put $test
in $_SESSION['whatever']
.
You'll need to session_start()
as the first thing in the php activity for both HTTP requests. In the second HTTP request, just json_decode()
the object and then add it to $_SESSION['whatever']
.
In case you're not familiar with sessions, they basically give you the ability to retain state, storing state data in $_SESSION[]
. See here: http://www.php.net/manual/en/function.session-start.php
You need to recreate $test
on the json page:
<?php
require_once 'test.php';
$test = new Test('some parameter');
echo json_encode($test->SomeFunction());
?>
These are two separate calls to PHP: the first one is your loading page (main.php) and the second is in the ajax call (start.php). Therefore, you need to require/include test.php again in your start.php.
add it to session on first request
$_SESSION['test'] = serialize($test);
and in start.php
$test = isset($_SESSION['test']) ? unserialize($_SESSION['test']) : 'no test';
and print it to repsponse
main.php
is requested from the server. All server side code is evaluated and executed, and a response is sent. At that point, the script is complete.
Some time after that, an AJAX request hits the server. This is a completely separate request, and any data that you need to recreate the class 'test' needs to be sent along with the request in GET
or set in SESSION
.
when you make an ajax request, you are starting a new PHP request, so none of the variable in one will be availble in the other unless passed explicity through JSON. you could store it in session, or you could pass your 'some parameter' string to the requested script and construct the Test object there.
HTTP is a stateless protocol, everything starts from scratch between requests.
You don't typically put objects in sessions as it can lead to unexpected behaviour. You should instead reinstantiate Test in start.php.
If Test needs to retain state (the values of some variables), you should break that state out and store it in a session on its own.
In main.php
, $test
is a global variable, which will exist until PHP is done executing. When the browser shows the page, that means PHP is finished executing.
When you make an AJAX call to start.php
, PHP starts again, and unless you include main.php
it has no idea about $test
.
You need to re-declare $test
on start.php
, or declare it in another file, and include it in both main.php
and start.php
.
精彩评论