CommonJS for PHP

Skip to first unread message

Dr. Benton

Sep 13, 2012, 2:24:09 PM9/13/12
Hi !

Just a word for people who love using CommonJS Module pattern but have to deal with PHP projects at work, like me :-)

I've created a light-weight CommonJS implementation for PHP (≈ 150 lines of code), available on GitHub : 
It has the following CommonJS features:
  • Modules run in a "Closure sandbox"
  • $require method, available in or outside Modules
  • in the Modules sandbox we have access to the following variables (and only to them):
    • the $require function
    • a $exports array, for exposing stuff
    • a $module array, with $module['id'] and $module['uri'] properties according to the CommonJS spec. One can also set a direct export with $module['exports'].
Furthermore, I have added 2 features inspired by RequireJS : a "defined-by-Closure" functionallity with a "$define" function, and Modules "plugins" with the syntax $require('[plugin name]![resource path']').
I also added a PHP version of Node's "Folder as Module".

I wish that it can be useful for others Javascript lovers who work with PHP too!


Christoph Dorn

Sep 13, 2012, 3:17:53 PM9/13/12


I built a prototype to bring some CommonJS concepts to PHP as well:

Rather than asking PHP developers to use so much of the JS-specific terminology (define, require, ...) I opted to focus on transferring the concepts.

You can map packages/dependencies using `package.json` [1] and use them with [2]:

  use lang-1/Greetings as Greets1;

The loader creates sandboxes for each package and builds on the PHP5 concept of namespaces to uniquely identify mapped packages. This is 100% compatible with the package/module isolation strategy CommonJS is trying to achieve.

The PHP loader is not up to date with the latest refinements (descriptor formats, layering, abstraction) I am working on as part of sourcemint [3] but I am planning on porting it soon.

I see room for both your and my approach and would welcome more discussion. I tried to setup a "commonphp" google group but keep getting an error. Maybe you have more luck.

I own the `` domain and was hoping to eventually publish some prototypes.


[1] -
[2] -
[3] -

Dr. Benton
13 September, 2012 11:24 AM
You received this message because you are subscribed to the Google Groups "CommonJS" group.
To view this discussion on the web visit
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

Reply all
Reply to author
0 new messages