The Nexus
Show navigation Hide navigation
  • BLOG
  • ABOUT
    • Ego Surfing
  • PROJECTS
    • n2
    • S2ajax
    • Condo
    • ezEdit
    • FreeBlog - Air
    • Tooredo Alpha
    • Tools
    • Journlr, Again!
7 Oct 2009 | 3 min. (429 words)

S2ajax v1.0 connects simply PHP and JavaScript

S2ajax Here 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.

Increment counter

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!

Contents

  • The concept
  • Under the hood
  • The Client’s point of view
  • The server’s point of view
  • A code sample
        • Server-side
        • Client-Side
  • Get it now!

Comments powered by Talkyard.

ClicDev: display last 24 hours visitors

PHP PDO class and XAMPP/"exotic" MySQL configurations

2022 The Nexus