Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6329441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:39:56+00:00 2026-05-24T17:39:56+00:00

What scope to php variables have with regards to AJAX calls? So for example,

  • 0

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. E.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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:39:59+00:00Added an answer on May 24, 2026 at 5:39 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an class with some instance variables. It's purpose is for templating. So
I have a function in a functions.php file that defines certain variables: add_action( 'the_post',
What is the scope of variables in javascript? Do they have the same scope
I have a 9000-line PHP file which consists of about 30 discrete areas, navigated
I have a php class that tells time like this (Time.class.php): <?PHP class Time
I have this function function get_last(){ $.ajax({ url:'fronta.php?get_last&last=1', success:function(data) { var last = parseInt(data);
If I set a Friend-level scope on a setter, like this... Public Class MyClass
I have a number of application settings (in user scope) for my custom grid
This should be obvious, but I'm getting a bit confused about PHP variable scope.
I've heard the latest PHP has support for namespaces. I know variables defined in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.