0

I would like to pass around a php class instance between PHP and JavaScript by using Ajax and jQuery.

I need to access data properties in JavaScript and execute methods in PHP. The following piece of code shows what I mean:

index.php

<?php require_once("php/foo.php") ?>
var bar = <?php echo json_encode(new foo()); ?>;

// do something with 'bar'
$.post("ajax/foo.ajax.php", { foo : bar }, function(data) {});

foo.ajax.php

require_once("php/foo.php);

if (isset($_POST["foo"]))
{
   $bar = $_POST["foo"];
   // do something with 'bar'
   echo json_encode($bar);
}

1 Answer 1

1

There really isn't a mechanism in PHP to accomplish that right now. You can use serialize and unserialize to retain object structure, and json_encode will pull all of the public properties out of your object, but json_decode will return it into a state of a StdObject (standard object) in PHP.

A workaround for this would be the iterate over the properties returned and reset them in your object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.