1 (edited by Lucas-Kane 2014-03-05 2:50:39 PM)

Topic: Passed object is suddenly gone

Hi,

i have been spending hours now to try to figure this out, but now i need help.

I have 2 classes.

in class 1 i have two functions.

the first function wich is called first, creates an object of the second class. then it uses $xajax->call to call the second function and pass the object as parameter. This is how it looks:

$response->call('xajax_Testclass.testFunction2',$this->object);

if i var_dump the object before i pass it, i get all the data, everything is where it should be.

the second function, where the object is passed to, looks like this:

public function testFunction2($object){
   
        $response = new xajaxResponse();
        $response->assign('test','innerHTML',$arg->getName());
        return $response;

$object is now empty.

Looking at firebug, i get the following message:

<b>Fatal error</b>: Call to a member function getName() on a non-object

Also, var_dump shows an empty array where before it showed an object.

Here is my question: How can i pass an Object using call from one function to the next?

The entire code here:


    public function testFunction (){
        $this->object = new SomeTestClass("Frank", "Schwarz");
        $response = new xajaxResponse();
        $response->call('xajax_Testclass.testFunction2',$this->object);
        return $response;
    }
   
    public function testFunction2($object){
        $response = new xajaxResponse();
        $response->assign('test','innerHTML',$object->getName());
        $response->append('test','innerHTML',$object->getFarbe());
        return $response;
    }

Re: Passed object is suddenly gone

You can't pass an object like this.  The PHP will have ended and destroyed everything before you come back in the second call. The way we handle this, is to pass the id of the database record around, and then use that to recreate the object.  Like this....

public function testFunction (){
        $this->object = new SomeTestClass("Frank", "Schwarz");
        $response = new xajaxResponse();
        $response->call('xajax_Testclass.testFunction2',$this->object->id);
        return $response;
    }
   
    public function testFunction2($id){
        $this->object = new SomeTestClass();
        $this->object->LoadFromId($id);
        $response = new xajaxResponse();
        $response->assign('test','innerHTML',$object->getName());
        $response->append('test','innerHTML',$object->getFarbe());
        return $response;
    }

Re: Passed object is suddenly gone

Hi Lucas,

You cannot pass an object that way. It will not survive the round trip. Use serialize() to convert it to a string and send it to the browser. Use unserialize() to convert it back to its original form.

Like this:

<?php
/**
 * 11:11 AM 3/5/2014
 * Example of passing objects
 */
//Setup the xajax framework.
include_once("xajax/xajax_core/xajax.inc.php");
$xajax = new xajax();
$xajax->configure('javascript URI', 'xajax/');
//$xajax->configure('debug',true);
class test2{
  public $first;
  public $last;
  
  function __construct($first, $last)
  {
    $this->first = $first;
    $this->last = $last;
  }

  function getName()
  {
    return $this->first.' '.$this->last;
  }
}

class testclass{
    //public $object;
    public function testFunction (){
        $object = new test2("Frank", "Schwarz");
        $response = new xajaxResponse();
        $response->call("xajax_testclass.testFunction2",serialize($object));
        return $response;
    }
    
    public function testFunction2($object){
        $response = new xajaxResponse();
        $ob = unserialize($object);
        $response->assign('test','innerHTML',$ob->getName());
        return $response;
    }

}

$tc = new testclass();
$xajax->register(XAJAX_CALLABLE_OBJECT,$tc);
$xajax->processRequest();
?>
<!DOCTYPE html>
<html>
<head>
<?php $xajax->printJavascript() ?>
</head>
<body>
<input type="button" value="Test" onclick="xajax_testclass.testFunction()"/></br>
<div id="test"></div>
</body>
</html>

You can use the response appendResponse() in xajax 0.5 or addCommands() in 0.6 to save an extra trip to the server.

$response->addResponse($this->testFunction2($object)); The object is passed OK and it gives you the same result as calling it from the browser.

Hope this helps.

Ed

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.

4 (edited by Lucas-Kane 2014-03-06 10:44:25 AM)

Re: Passed object is suddenly gone

@edrobinson

$response->addResponse($this->testFunction2($object)) does not work. neither in 0.5, nor in 0.6 beta wich i downloaded now.

Not sure if i downloaded the right 0.5 version from your website. Its xajax 0.5 standard.

However, your solution itself worked quite well.

-----
Here is my final Code, hope it helps with Future questions of a similar kind:

--index.php--

<?php
include 'xajax_core/xajax.inc.php';
require_once 'Testclass.class.php';
require_once 'Sometestclass.class.php';
$xajax = new xajax();
$testclass = new Testclass();
$xajax->register(XAJAX_CALLABLE_OBJECT, $testclass);
$xajax->processRequest();

?>
<!DOCTYPE html>
<html>
    <head>
        <?php $xajax->printJavascript(); ?>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <script type="text/javascript">
        xajax_Testclass.testFunction();
        </script>
        <div id="test" name="test">
        </div>
    </body>
</html>

--testclass.class.php--

<?php

class Testclass {

 
    private $response; 
    private $object;

    public function __construct(){
        $this->response = new xajaxResponse();
        
    }
    
public function testFunction (){
        $this->object = new SomeTestClass("Frank", "Schwarz",1);
        $response = new xajaxResponse();
        $response->call('xajax_Testclass.testFunction2',  serialize($this->object));
        return $response;
    }

     public function testFunction2($id){
        
        $obj = unserialize($id);
        $response = new xajaxResponse();
        $response->assign('test','innerHTML',$obj->getName()."<br>");
        $response->append('test','innerHTML',$obj->getFarbe());
        return $response;
    }
    
}

--sometestclass.class.php--

<?php

class SomeTestClass {
    
    private $name;
    private $lieblingsfarbe;

    
    public function __construct($name, $farbe) {
        $this->name = $name;
        $this->lieblingsfarbe = $farbe;
    }
    
    public function getName(){
        return $this->name;
    }
    
    public function getFarbe(){
        return $this->lieblingsfarbe;
    }
    
}

This, however was just step one. I started this, because I have a Guestbook wich reads entries from a Database. Each entry becoms an object which i then want to pass to another class wich has the objective to put the entries on the screen. With the help you provided, it should work. Thanks a lot!

Edit:

The Guestbook now finally works only on XAJAX. The Database entries are received and displayd via XAJAX utilizing php functions and the entries i write are being written into the Database, also only with xajax. Powerful little tool.. I learned how to use ajax and i have to say it makes things A LOT easier.