With such post title you may think that doctor was a shrink ... well, I let you figure it out after reading ;)
About Hybrid Programming Languages
We all know CoffeScript, right? As well as Traceur Compiler ... right? These are what I call Hybrid Programming Languages, or better, those kind of pseudo representation of the programming language we use, in this case still JavaScript, passing through a more abstract, enhanced, "improved" syntax (Coffee) or language features (Traceur) .About PHP And JavaScript
If you follow me since years you may have noticed how many times I have tried to make PHP more JavaScriptish. JavaScript like Object class is the first example from 2008, while [js.php] A JavaScript Object Like PHP Class was surely a better attempt, thanks to new PHP 5.3 features, as well as PHP JavaScript like Number class, this time based on PECL operator overloads.I Am Not Alone
What I could not expect is that such "wanna write PHP as if it is JavaScript" topic is an obsession for many other developers such Tantek Çelik with his CASSIS or Stoyan Stefanov via JavaScript-style object literals in PHP.As somebody commented already in one or more related posts, "why on earth do not use simply node.js ?".
Well, if node.js had even half of PHP hosts support, I am pretty sure many of us would have already solved this problem ... unfortunately node.js is "not that ready yet" and for some reason hosters prefer whatever version of PHP rather than a faster and more fashionable node.js.
JHP: JavaScript Hypertext Preprocessor
The name JHP is not new for me. I have used the same name many months before projects like php.js were announced.While latter project aim is to bring to JS functionalities from PHP, I have revolutionized a bit my own concept of JHP trying to bring into native PHP world all JS possibilities ... here some example:
// $Object and $Function
$o = $Object();
$o->name = 'JHP';
$o->toString = $Function(function ($self) {
return $self->name;
});
echo $o; // JHP
$Object->prototype->toString->call($o); // [object Object]
$me = $Object();
$me->name = 'WebReflection';
$me->hasOwnProperty('name'); // true
$o->toString->call($me); // WebReflection
$o->toString(); // JHP
$f = $o->toString;
$f($me); // WebReflection
// $String
$str = $String('abc')->concat('d', 'ef', $String('g'));
echo $str; // abcdefg
echo $Object->prototype->toString->call($str); // [object String]
// $String is an object
// so it's possibe to define properties (this is a getter)
$Object->defineProperty($str, 'name', (object)array(
// just recicle the method inherited from $String->prototype
'get' => $str->toString,
// set propeties
'enumerable' => false,
'writable' => false,
'configurable' => false
));
echo $o->toString->call($str); // abcdefg
// invoking $str->name as getter
// passing then through $str->toString callback
$a = $Array();
$a->push(1, 2, 3);
echo $a->length; // 3
$a->forEach(function ($value, $i, $arr) {
echo $value.PHP_EOL;
});
// uh, and require as well ...
$require('sys')->print('Hello World');
XMLHttpRequest Example
This is just what's already possible to extend some global.
// sync only ... well, it's PHP
$XMLHttpRequest = $Function();
$XMLHttpRequest->prototype->open = $Function(function ($self, $mode, $uri, $async = false) {
$self->_uri = $uri;
$self->_mode = $mode;
});
$XMLHttpRequest->prototype->send = $Function(function ($self, $data = null) {
// actually with curl we may emulate post, get, etc ...
$self->responseText = file_get_contents($self->_uri);
});
$xhr = $XMLHttpRequest();
$xhr->open('get', 'http://webreflection.blogspot.com/', false);
$xhr->send();
echo $xhr->responseText; // this blog ... somehow
$Array, $Boolean, $Function, $Number, $Object, $String
This is almost all I have so far and everything is absolutely in a prototype/experimental status.This is why I have not created a repository yet, also because this project was abandoned but just recently I have read the post from phpied.com so I thought ... well, why not talk a bit about my good old experiments as well? Maybe there is something to learn, surely there is a lot to improve, also thanks to latest PHP SPL and ... well, I let you decide what to do with this folder well forgotten in my good old netbook. You tell me if it makes sense, if you like the idea, and accordingly with feedbacks I will eventually consider to re-think from the scratch the whole pile of code in order to make it better, faster, usable, etc etc ... ok?
What's Missing
First of all ... to refactor everything, since at the beginning I remember I was trying to obtain a fresh new environment, or better, its globals, but I trapped myself without bringing the possibility to reuse current globals in a proper way.
Said that, the thing that I have never even started to do, as example, is a Narcissus based parser able to transform for me all JavaScript into JHP, bringing via use all nested scopes so that everything will be automatically available and we can really write JS and deploy on a PHP host ... this sound cool, uh? Well, let me tell you something, PHP is not that bad but such alien cannot surely be fast ;)
Last, but not least, the require uses eval and I hate eval, even if it's damned handy in some case.
How To Test / Where Is JHP
Well, it's here: have fun with JHP!
6 comments:
You are 1 crazy guy, Andrea. I mean that in a good way, but forgive me if I don't understand JHP.
FYI, JSMin+ is a PHP port of Narcissus. It works but the consensus is it's VERY memory intensive.
Honestly, I'm more interested in your health status than in JS or other machine topics. I'm glad to hear that you're recovering from your accident. Take care. We need you at 100%. :-)
I've been working on a similar project. But instead of targeting PHP I chose C++ :)
http://blog.vjeux.com/2011/javascript/jspp-morph-cpp-into-javascript.html
For all those who don't understand what PHP stands for it is Hypertext Preprocessor.
If you want to learn more about PHP here. Visit this url.
http://www.techyv.com/article/basics-php
HI there,
php hypertext preprocessor is a server-side HTML embedded scripting language. since HTML can only send and post data. Php is the one who manipulates data into database.
Thanks.
Post a Comment