S2ajax v1.0 connects simply PHP and JavaScript

S2ajaxHere comes S2ajax v1.0!
And it was long overdue. Six months already since I posted S2ajax says “hi()” I can hardly believe it.

What I think of as v1.0’s main feature is that it is now possible to simply export classes in PHP and these classes can be instantiated in JavaScript. Whenever these instances are modified through asynchronous method calls, these modifications are transparently persisted server-side.

The concept

Is it a PHP class? Is it a JavaScript class? Why, it’s both! The class is defined in PHP on the server. Instances of the class are created on demand using JavaScript on the client. Whatever modifications are made to an instance are serialized on the server.
You can create a complex application using as many classes and instances as you need.

100

Under the hood

The PHP class is exported; the proxy JavaScript code is generated.
Whenever the client needs to access one of the class’ properties/methods, the proxy transparently talks to the class; the class lives server-side.

99

The Client’s point of view

An arbitrary number of instances of the class can be created in JavaScript.
The only hint that you are using a client-server architecture is the fact that when invoking a method, its return value is obtained through a callback. This mechanism allows your client interface to remain responsive while the server is preparing a response.

101

The server’s point of view

S2ajax automatically persists your instances’ state and data between consecutive asynchronous calls. You still get the benefits of the “shared nothing” approach of PHP but complex objects can be manipulated through an unlimited number of clients requests.

102

A code sample

Server-side

Let’s create a class that will increment an instance variable every time a method is invoked. Let’s keep it as simple as possible:

class CounterTester
{
    private $counter;
 
    function __construct() {
        $this->counter = 0;
    }
 
    public function increment_counter() {
        $this->counter++;
        return $this->counter;
    }
}

Clearly, every time increment_counter() is invoked, $counter will be incremented and its new value returned.

Client-Side

First, an instance of our class is created. Then, when a user click on the button labeled ‘Increment counter’, the instance’s increment_counter() method will be invoked and the new $counter value returned to our callback and displayed.

?View Code JAVASCRIPT
<script>
function display_result(val) {
    alert(val); // Display new counter value
}
var counter = new CounterTester();
</script>
<button onclick="counter.increment_counter(display_result);">Increment counter</button>

Note that we could create as many instances of our class as we wish and provided we display the matching buttons, we could independently increment multiple counters.

Get it now!

Click our valiant friend “Octocat”, artistically represented below, to go to S2ajax’s Github page. If you just wish to use the library, look for the [Download] button:

Git!

Git!


Similar Posts:

If you enjoyed this post, make sure you subscribe to my RSS feed!

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

[...] the original post: S2ajax v1.0 connects simply PHP and JavaScript By admin | category: counter script | tags: display, function-display | Blackberry [...]

I spend some minutes testing it, i got a problem but i don’t think the library has anything to do with it.

I create an instance of class test, inside the constructor i instiantied another class SQL (to handle the conection to the SQL server), everithing goes fine, the query gets executed and i get the corect date and time, but if I push the button again, it does’t work because the SQL class is still instiantated, but the connection to the SQL gets lost, so I suspect that I need to review the SQL class because there must be my the problem.

Otherwise this 1.0 version is great. Is exactly what I needed, a very simple to use class that transports JS to PHP.

So no luck with modemmethod, I didn’t see the new class posted in their site.

here is the important parts of code that I used for testing:
s2ajax_export_class(
test,
array(
‘getNow’,
‘getSomething’
)
);
s2ajax_handle_client_request();

class test
{
private $db;
private $test;
function __construct(){
$this->db=new SQL();
$this->test=’someString’;
}

public function getNow() {
$sqlquery=’select now()’;
$sqlresults=$this->db->Select($sqlquery);
$tab = ‘Now is ‘.$sqlresults[0][0];
return $tab;
}

public function getSomething() {
return “some String = “.$this->test;
}
}

?>

html
head
script

function display_result(val) {
alert(val);
}

var test = new test();
script
body
button onclick=”test.getNow(display_result);”>getTime /button
button onclick=”test.getSomething(display_result);”>getString /button
body
html

You know, I could swear that I wrote _somewhere_ about this limitation. But I’ve been looking and I cannot find it anywhere…
Fact is, when serializing a PHP class, resources are lost. Therefore, if your class uses resources, which happens quite a lot with databases, you have to recreate them with each invocation.
The downside is obvious: you need to write a bit more code and there is some overhead. Now, the overhead could be taken care of by using persistent connections.
Remains the issue of having to execute some code whenever an object is accessed. It’s not ideal but you could, at least, put that code in the magic methods __sleep() and __wakeup(): use __sleep() to store a list of resources to re-acquire and __wakeup() would do just that.
If you find the idea helpful, there may even be a way to automate this sort of task in the framework…

Leave a comment

(required)

(required)