tags:

views:

117

answers:

6

i have instantiated a class in my index.php file. but then i use jquery ajax to call some php files but they can't use my object that i created in the index.php file.

how can i make it work? cause i don´t want to create new objects, cause the one i created holds all property values i want to use.

+1  A: 

Since you have not provided your code, but what i guess is that you need to make your instantiated object global for other scripts to see it, example:

$myobject = new myobject();

Now i want to use this object elsewhere probably under some function or class, or any place where it is not getting recognized, so i will make this global with global keyword and it will be available their as well:

global $myobject;

Once you have the object, you can put it into the session and then utilize on the ajax script file.

Sarfraz
+3  A: 

mm, you should store in session $_SESSION["someobj"] = $myobj; and ensure that when you call the ajax php file this include the class necesary files wich defines the class of $myobj and any contained object in it.

could you be more specific? i can try try

how so i create an object then assign it to a session variable.

include(whateverfilethathastheclassorincludeit.php)
$theObject = new TheObjectClass();
//do something with the object or not
$_SESSION['myobject'] = $theObject;

how do i do to access the object's members in my ajaxcall php file

include(whateverfilethathastheclassorincludeit.php)
$theObject = $_SESSION['myobject'];
//do something with the object
useless
could you be more specific? how so i create an object then assign it to a session variable. how do i do to access the object's members in my ajaxcall php file?
weng
i hope that explains it
useless
Hm. I'm not sure whether I approve of the use of the `<kbd>` tag for non-key-related issues :) but still +1.
Pekka
+1  A: 

If you don't want to move your object that is in your index.php, have your ajax make a request to index.php but add some extra parameters (post/get) that let your index.php know to process it as an ajax request and not return your normal web page html output.

Harley Green
+9  A: 

Use the session to save the object for the next page load.

// Create a new object
$object = new stdClass();
$object->value = 'something';
$object->other_value = 'something else';

// Start the session
session_start();

// Save the object in the user's session
$_SESSION['object'] = $object;

Then in the next page that loads from AJAX

// Start the session saved from last time
session_start();

// Get the object out
$object = $_SESSION['object'];

// Prints "something"
print $object->value;

By using the PHP sessions you can save data across many pages for a certain user. For example, maybe each user has a shopping cart object that contains a list of items they want to buy. Since you are storing that data in THAT USERS session only - each user can have their own shopping cart object that is saved on each page!

Xeoncross
+3  A: 

Another option if you dont want to use sessions is to serialize your object and send it through a $_POST value in your AJAX call. Not the most elegant way to do it, but a good alternative if you don't want to use sessions.

See Object Serialization in the documentation for more informations.

AlexV
A: 

As others have suggested, $_SESSION is the standard way to do it, in fact, that was one of the reasons, that sessions where invented to solve. Other options, i.e. serializing the object rely on the client side to hold the object and then return it untampered. Depending on the data in the object, it is not a good solution, as a) the object may include information that should not be available on the client side for security reasons and b) you will have to verify the object after receiving it.

That said, and if you still want to use the object on the client side, then JSON is an option for serializing object data, see JSON functions in PHP.

Residuum