Fork me on GitHub

Registering PHP classes


Before a PHP class or function can be called from javascript, it must first be registered. The register() function of Jaxon is used for that purpose.

A class is registered with Jaxon by calling the register() function with the Jaxon::CALLABLE_CLASS parameter.

use Jaxon\Jaxon;
use Jaxon\Response\Response;

class HelloWorld
{
    public function sayHello($isCaps)
    {
        $response = new Response();
        $text = ($isCaps) ? 'HELLO WORLD!' : 'Hello World!';
        $response->assign('div2', 'innerHTML', $text);
        return $response;
    }

    public function setColor($sColor)
    {
        $response = new Response();
        $response->assign('div2', 'style.color', $sColor);
        return $response;
    }
}
$jaxon = jaxon();
$jaxon->register(Jaxon::CALLABLE_CLASS, HelloWorld::class);

After the class is registered, all its public methods are available in a javascript class named JaxonHelloWorld. The prefix Jaxon is defined using the core.prefix.class configuration option.

Here's the javascript code Jaxon will generate.

JaxonHelloWorld = {};
JaxonHelloWorld.sayHello = function() {
    return jaxon.request(
        { jxncls: 'HelloWorld', jxnmthd: 'sayHello' },
        { parameters: arguments }
    );
};
JaxonHelloWorld.setColor = function() {
    return jaxon.request(
        { jxncls: 'HelloWorld', jxnmthd: 'setColor' },
        { parameters: arguments }
    );
};

Here's an example of HTML code that calls this class.

<input type="button" value="Say Hello" onclick="JaxonHelloWorld.sayHello(0)" />
<input type="button" value="Set Color" onclick="JaxonHelloWorld.setColor('red')" />

Setting options on Jaxon requests

Additional options can be passed to classes when they are registered, and included in generated javascript functions. They allow to change the behavior of Jaxon requests, without changing the way they are called.

In practice, these options are included into the javascript functions generated by Jaxon.

// Classes options
$jaxon->register(Jaxon::CALLABLE_CLASS, HelloWorld::class, [
    'setColor' => [
        'name' => "'value'"
    ],
    '*' => [
        'name' => "'value'"
    ]
]);
// Functions options
$jaxon->register(Jaxon::CALLABLE_FUNCTION, "hello_world", [
    'name' => "'value'"
]);

The options are defined in an array whose indexes are their names. Note that when defining an option of type string, quotes are included in its value.

For classes, each option group is indexed by the name of the method they apply to, or by * if the options apply to all methods.

For example, the mode option indicates whether the Jaxon requests are asynchronous or not.

$jaxon->register(Jaxon::CALLABLE_CLASS, HelloWorld::class, [
    'setColor' => [
        'mode' => "'synchronous'"
    ],
    '*' => [
        'mode' => "'asynchronous'"
    ]
]);

Here's the generated javascript code.

JaxonHelloWorld.sayHello = function() {
    return jaxon.request(
        { jxncls: 'HelloWorld', jxnmthd: 'sayHello' },
        { parameters: arguments, mode: 'asynchronous' }
    );
};
JaxonHelloWorld.setColor = function() {
    return jaxon.request(
        { jxncls: 'HelloWorld', jxnmthd: 'setColor' },
        { parameters: arguments, mode: 'synchronous' }
    );
};