Highlights

  • Declarative and unobstrusive HTML instead of imperative Javascript
  • AJAX file uploads for good browsers with no effort, just make the form pheryfied
  • Really small footprint, no external dependencies beside jQuery
  • Coded with performance and security in mind
  • Code, server and client side, completely commented, all methods have docs
  • Javascript config locking and Object.freeze to make it harder for tampering
  • The same way you write jQuery in Javascript, you write it in PHP
  • Turns static Javascript app code into a dynamic language with a chainable query builder style
  • Unit tested, mocha for the Javascript and PHPUnit_Selenium for PHP
  • Support for old, new and upcoming jQuery functions out of the box, since nothing is hard coded
  • You may nest PheryResponses the way you would with jQuery
  • You may set Javascript callbacks directly from PHP
  • You can call jQuery plugin functions directly from PHP (think colorbox for example)
  • Built-in CSRF mechanism that makes each AJAX request signed
  • Inline conditional execution using unless or incase
  • Access or call any Javascript code on the page, set/unset variables, add functions to the page on-the-fly
  • Plays really well as the transport for client-side frameworks and libraries as such AngularJS, Ember.js, Knockout.js, ExtJS, Backbone

Wanna see how it looks like?

<?php
include "Phery.php";

Phery::instance()->set(array(
  // You don't need to use lambda functions, but it's the power of PHP 5.3 ;)
  'alias-for-function' => function($ajax_data){
    ob_start();
    var_dump($ajax_data);
    $data = ob_get_clean();

    return
    PheryResponse::factory('#result')
      ->html($data) // Acts like $('#result').html($data)
      ->jquery('body') // Acts like $('body').css('backgroundColor', 'red')
      ->css(array('backgroundColor' => 'red'))
      ->alert('This is an alert') // Acts like alert('This is an alert')
      ->call('javascript_function', 'arg1', 'arg2', 3, array(1,2,3)) // Acts like javascript_function('arg1', 'arg2', 3, [1,2,3])
      ->this // Acts like $(this).height(100).width(100) and  "this" refers to the calling element on the page
      ->height(100)
      ->width(100)
      ->jquery // Acts like $.getJSON
      ->getJSON('http://jsonurl', PheryFunction::factory(array(
        'function(data){',
          'console.log(data);',
        '}'
    )));
  }
))
->process();
?>
<!doctype html>
<html>
<head>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
  <meta charset="utf-8">
  <title>Phery</title>
  <script src="phery.js" type="text/javascript"></script>
</head>
<body>
  <?php echo Phery::link_to('Click me', 'alias-for-function', array('class' => 'className', 'args' => array('data' => 'hallelujah'))) ?>
  <div id="result"></div>
</body>
</html>

Introduction

The main goal of this library is to make jQuery (and Javascript), be completely dynamic instead of static and unchangeable code, returning commands on-the-fly from the server and execute them in order on the client, while still manipulating the DOM and dealing with callbacks.

This library unleashes everything you expect from an AJAX library, with nested responses, merging and unmerging responses, direct access to the DOM element that is making the AJAX call. It's loosely based on jquery-ujs for Rails concept.

It's a straightforward and powerful AJAX library with direct integration and mapping of all jQuery functions in PHP, the mapping is extended to custom functions set by $.fn, can create elements just like $('<div />') does, as phery.js creates a seamless integration with jQuery functions, through AJAX to PHP functions, as you can call a PHP function using AJAX

All jQuery functions listed in here are available to call directly from the PheryResponse class: http://api.jquery.com/ including any new versions of jQuery that comes out, its compatible with jQuery forever, since there are no hardcoded jQuery functions in the PHP code. No need to update phery.js, as it will continue to work with future versions of jQuery automagically (unless jQuery change anything in core that breaks any existing function).

phery.js uses HTML5 data attributes to achieve this, and no additional libraries (besides jQuery, since it's a plugin) are needed, even for Internet Explorer. Links and forms will still be able to send GET/POST requests and function properly without when no Javascript isn't enabled, because it doesn't use obtrusive techniques and relies on event delegation.

Strict standards for PHP 5.3+ and advised to use jQuery 1.8+. Being just one PHP file, and one Javascript file, it's pretty easy to carry around or to implement in PHP auto-load scenarios, plus it's really FAST! Average processing time is around 2ms with vanilla PHP, according to Firebug and in the demo page

The magic_quotes_gpc directive is DEPRECATED in 5.3 and REMOVED in 5.4, since you are always 100% responsible for the security of your data, so escape your text accordingly to avoid SQL injection or XSS attacks.

All AJAX requests are sent as POST only, so it can still interact with GET query strings, like paginations and such (?p=1 / ?p=2 / ...).

The code is mostly commented using phpDoc and jsDoc, for a steep learning curve, using doc-enabled IDEs, like Netbeans , Aptana , Phpstorm and Eclipse based IDEs. Also, most of the important and most used functions in jQuery were added as phpDoc (from http://api.jquery.com/ ), as a magic method of the PheryResponse class.

What do I need?

  • PHP 5.3.3+
  • jQuery 1.8+
  • Firefox 3.6+, IE 8+, Chrome 12+, Safari 5+, Opera 10+

Leave a comment

comments powered by Disqus