» PHP.JS Namespaced

PHP to Javascript Project: php.js

php.jsThis article is part of the 'Porting PHP to Javascript' Project, which aims to decrease the gap between developing for PHP & Javascript.

A lot of people are familiar with PHP's functions, and though Javascript functions are often quite similar, some functions may be missing or addressed differently. The Javascript implementations should be as compliant with the PHP versions as possible, a good indication is that the PHP function manual could also apply to the Javascript version.

Porting crucial PHP functions to Javascript can be fun & useful. Currently some PHP functions have been added, but readers are encouraged to contribute and improve functions by adding comments. Eventually the goal is to save all the functions in one php.js file and make it publicly available for your coding pleasure.

If you choose to contribute, let me know how you want to be credited in the function's comments. You may also want to subscribe to RSS so you receive updates whenever new functions are posted.

Thanks to a lot of extra effort by Michael White (http://crestidg.com) there now is a namespaced version of PHP.JS available for your coding pleasure.

Namespaces

Namespaces are used to group functions & objects together. In this case, all PHP.JS functions are contained in one big object: PHP_JS.

Look at this code (not namespaced):

// call a PHPJS function
p = strpos('Kevin van Zonneveld', 'e');
 
// call another PHPJS function
d = date('F j, Y, g:i a');

Easy typing right? Just like you know from PHP.

Now look at this code (namespaced):

// create PHPJS object
window.$P = PHP_JS();
 
// call a PHPJS function
p = $P.strpos('Kevin van Zonneveld', 'e');
 
// call another PHPJS function
d = $P.date('F j, Y, g:i a');

Little bit different. All PHP.JS code is now stored in it's own object. Some people like that because it can then never conflict with any existing function names (either by you, your CMS, other libraries or even JavaScript itself). This becomes clear with abs:

a = Math.abs(-7.25); // Javascript's own abs() function
a = $P.abs(-7.25);   // php.js's abs() function
 
a = abs(-7.25);      // only recognized when using normal php.js

Other namespacing benefits

Namespacing makes PHP.JS more flexible and makes it easier for developers to extend and include this library. They have more control over this project because it listens to a single name. If you want to dig a little deeper, try wiki.

Unnamespaced PHP.JS

Both unnamespaced & namespaced versions will be maintained and updated.

Why? I know that there are a lot of people out there who do not want to switch to a namespaced variant because they just want to be able to call the functions directly. For the ease of typing, but also because it resembles PHP, and that's the whole point of this project, right?

Download php.js

You can do that on every function's page. But for your convenience:

Future features

We're currently working on two major project improvements.

Better Versioning

All functions are versioned, but we're working on solid versioning of the main php.js file as well, this feature will be ready really soon and announced here.

Lightweight

PHP.JS contains a lot of functions and you probably won't use them all in one site. So to people who are concerned with things like bandwidth I can recommend a couple of things:

The last suggestion is not very developer-friendly though. You will survive but still.. We've got the idea to create an online application with a lot of check boxes and a couple of default presets. To allow you to exclude all kinds of functions and totally customize your own PHP.JS. Your unique combination of functions will additionally generate a hash, so you can always enter that hash again to download the newest versions of your favorite functions.

Dedicated site

We're currently working on a dedicated site for PHP.JS. Moving this project away from my Blog will make it more accessible and help professionalize the project.

New to PHP.JS?

If you're new to PHP.JS, checkout an example. We are not trying to port or emulate the entire language or control structures of PHP. We don't see the need because JavaScript seems to have more elegant features in that category anyway.

However in our eyes, PHP does provide a large set of standard functions that make developing very easy, and some of them don't have good standard JavaScript implementations, though they often would be great to have client-side.

So in this project by also providing the functions separately, we hope to keep people from inventing the wheel and give them a head start.

About the project

The PHP.JS project started in December 2007. It quickly became populair and within 4 months PHP.JS supported over 120 PHP functions all translated to JavaScript altenatives. PHP.JS is open source and has received (and still is receiving) code contributions from 50+ developers around the world, who have all helped to make PHP.JS what it is today. A rich Javascript library that adds functionality & PHP compatibility to your projects.


Like this article?

   Then Dzone it!
Or use another bookmark button below to show your support &
help me spread the word.


tags: programming, javascript, php, phpjs
category: Programming - Javascript - PHP equivalents
read: 2,699 times

Add comment

for syntax highlighting

[CODE="Javascript"]
your_code_here();
[/CODE]

Replace "Javascript"
with "php", "text", etc.
code (to make sure you are not a spammer)

Comments

#7. Sincklation on 17 April 2008

SincklationGreat Work !

#6. Kevin on 08 April 2008

Kevin@ Weston Ruter: Yep, we're in the midst of moving php.js to its own publicly available SVN repository (not Google, but still). We're also still in negotiation and hope to obtain the phpjs.com domain name, so we can build a dedicated site around this project. It should resemble the php.net site and offer special features like customizing your own php.js version for the functions you need, etc.

#5. Weston Ruter on 08 April 2008

Weston RuterWhat about hosting this PHP.JS project on Google Code? It'd be great to have a more dedicated location to this great effort.

#4. Kevin on 02 April 2008

Kevin@ _argos: Good to have you back waldo :) Two great contributions to this project, awesome! thanks

#3. _argos (waldo malqui silva) on 01 April 2008

_argos (waldo malqui silva)Hi Kevin: I'm back :p, for my work I need these 2 functions ip2long and long2ip I sent U.

function ip2long ( ip_address ) {
    var output = false;
 
    if ( ip_address.match ( /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ ) ) {
      var parts  = ip_address.split ( '.' );
      var output = 0;
 
      output = ( parts [ 0 ] * Math.pow ( 256, 3 ) ) +
               ( parts [ 1 ] * Math.pow ( 256, 2 ) ) +
               ( parts [ 2 ] * Math.pow ( 256, 1 ) ) +
               ( parts [ 3 ] * Math.pow ( 256, 0 ) );
    }
 
    return output;
}
 
  function long2ip ( proper_address ) {
    var output = false;
 
    if ( !isNaN ( proper_address ) && ( proper_address >= 0 || proper_address <= 4294967295 ) ) {
      output = Math.floor (proper_address / Math.pow ( 256, 3 ) ) + '.' +
               Math.floor ( ( proper_address % Math.pow ( 256, 3 ) ) / Math.pow ( 256, 2 ) ) + '.' +
               Math.floor ( ( ( proper_address % Math.pow ( 256, 3 ) )  % Math.pow ( 256, 2 ) ) / Math.pow ( 256, 1 ) ) + '.' +
               Math.floor ( ( ( ( proper_address % Math.pow ( 256, 3 ) ) % Math.pow ( 256, 2 ) ) % Math.pow ( 256, 1 ) ) / Math.pow ( 256, 0 ) );
    }
 
    return output 
  }

#2. Dharmavirsinh Jhala on 29 March 2008

Dharmavirsinh JhalaGreat work...!
PHP.JS has been part of my common javascript library and now it has namespace too..
http://blogs.digitss.com

#1. BTM on 29 March 2008

BTMVery nice, I can see that I will be using some of the funcions ported in my projects from now on :D