Good programming practices

Hybrid class with JavaScript and PHP

January 9th, 2011

JavaScript and PHP are very often used together in one project. In order to facilitate programming with these two languages I created a library that allows you to implement part of the class in JavaScript, and the rest in PHP. In this way, you can call a JavaScript method written in PHP. The developer can focus on creating code, and synchronization is done by the library.

Applications

Objects in JavaScript are client-side, allowing you to interact with the user. Logical layer remains on the server side.

  • Create and modify objects in the GUI, which are next written in PHP, for example in a database.
  • Validation of data entered by the user.
  • Hiding parts of the implementation from the user.
  • Use features not available in JavaScript.

Example

Simple implementation of a bank account. Account transactions are caused by JavaScript, but it’s done by PHP. An object in a browser has the correct status both before and after transaction. PHP script stores the state of your account. To simplify the example, the validation of the data is omitted here.

PHP   
  1. include_once 'phpjs/phpjs.php';//path to file
  2.  
  3. class BankAccount extends PhpJs {//must extends PhpJs
  4. private $amount; //private - will not be send to JS
  5. protected $number; //protected - will be send to JS
  6.  
  7. function init(){ //executed when JS object is created
  8. if($this->number) {
  9. $this->amount = $this->getAmount();
  10. }
  11. }
  12.  
  13. function widthdraw($amount){
  14. $this->amount = $this->getAmount() - $amount;
  15. $this->save();
  16. }
  17.  
  18. function deposit($amount){
  19. $this->amount = $this->getAmount() + $amount;
  20. $this->save();
  21. }
  22.  
  23. function getAmount(){
  24. return @file_get_contents($this->number);
  25. }
  26.  
  27. private function save(){ //private - will not be send to JS
  28. $fp = fopen($this->number, "w");
  29. fwrite($fp, $this->amount);
  30. fclose($fp);
  31. }
  32. }
  33.  
  34. $account = new BankAccount(); //make general instance
  35. echo $account->phpJs(); //communicate with JS
  1. function BankAccount(number) {//constructor
  2. this.number = number; //assign variable
  3. this.phpFile = 'phpjs/bankaccount.php'; //set file with your PHP class
  4. BankAccount.inherit(PhpJs.call(this)); //inherit the PhpJs
  5. }
  6.  
  7. var account = new BankAccount(1234);
  8. document.writeln("Begin="+account.getAmount());
  9. account.deposit(220);
  10.  
  11. var accountProxy = new BankAccount(1234); //the same number
  12. document.writeln("+220="+accountProxy.getAmount());
  13.  
  14. account.widthdraw(70);
  15. document.writeln("-70="+account.getAmount());
  16.  
  17. document.writeln("Proxy="+accountProxy.getAmount());

The result of the above code is:

Output   
  1. Begin=false
  2. +220=220
  3. -70=150
  4. Proxy=150

You will find this example is along with the library in the file at the bottom.

Technologies

The project uses several technologies:

  • Synchronous (as opposed to AJAX) calls to XMLHttpRequest.
  • JSON – data exchange format.
  • PHP reflections.

Modifications

This is a very early version of the project, therefore, the way of using can change. To do is:

  • optimizing the number of queries when creating multiple objects,
  • static methods
  • better error handling.

Summary

This library enhances the readability of the code, and relieves the need for manual synchronization of objects between server and client. It is very small, the 3kB in your JS and PHP. The disadvantage of the library is synchronous communication with the server, and sending request to it each time a method written in PHP is invoked by JavaScript.

Download

Library and example PhpJs Hybrid Class 0.1

Leave a Response

You must be logged in to post a comment.