开发者

Any Javascript and PHP AJAX Interface generation suggestions, like thrift?

I am basically looking for Apache Thrift, but to talk between JavaScript over Ajax and PHP. I know Thirft generates both, but to my knowledge the JavaScript code must talk over JSONProtocol, of which the protocol isn't yet wrote in PHP.

Are there any other alternatives 开发者_运维知识库that can suggested?

If you are unfamiliar with Thrift, this is a simple(ish) definition of what i need:

Consider this as the generic interface definition language (IDL), where I setup a User object, an AuthenticationResult result object, and method named UserCommands.Authenticate();

struct User {
    1:  number          id,
    2:  string          firstName,
    3:  string          lastName
}

struct AuthenticationResult {
    1:  number              currentTime,
    2:  User            user
}

service UserCommands {
    AuthenticationResult        Authenticate(1:string username, 2:string password)
}

I run a program or something, it creates JS and PHP libraries based on the above.

Then, in JS, I could call (with helpful typehinting).

var myAuthResult = UserCommands.Authenticate('myUser', 'myPass');
alert ("My first name is : " + myAuthResult.user.firstName);

And in PHP, I would setup a method in a UserCommands class like this:

function Authenticate($username, $password) {
   $myUser = new User();
   $myUser->firstName = "Fred";
   $myUser->lastName = "Thompson";
   $myAuthResult = new AuthenticationResult ();
   $myAuthResult->currentTime = date("U");
   $myAuthResult->user = $myUser;
   return $myAuthResult;
}

The benefits are that PHP can return native objects and JS can expect to receive its own native objects. Type hinting for available methods are provided through out, with expected params and return results.

Any thoughts would be appreciated!


First of all, there're json_encode and json_decode functions in php.
Secondly, there's a serialize/unserialize for native php types
I don't understand, though, which you mean under "... of which the protocol isn't yet wrote in PHP."

Also, there's a Haxe langauge, which can be "compiled" into both PHP and JavaScript (and some other languages)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜