开发者

Possibility of implementing node.js using PHP interpreter in place of JavaScript v8? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 4 years ago.

Improve this question

I have a command line application developed in PHP that currently utilizes files and DB content for input. I have need for more universal interoperability and have stumbled upon Node.js. I see that it would allow me to very quickly create a RESTful or SOAP interface that would be scalable on the front end with minimal effort.

Knowing it is written in C and uses the JavaScript V8 I was wondering if any thought by anyone has been put into doing a parallel implementation of node.js as say, node.php? PHP developers would gain the wonders of this framework and of course I could take immediate advantage with limited change开发者_StackOverflows to my current code base.

What might be the feasibility, viability and potential challenges to such an implementation? How rooted in the JavaScript engine within Node.js and could it easily be replaced with a PHP engine?


If all you're interested in is to "very quickly create a RESTful or SOAP interface" in PHP, then you may be looking in the wrong direction.

Rather than looking for a reimplementation of V8 + node.js in PHP, there are numerous existing PHP frameworks out there that offer similar abstractions. See: http://www.noupe.com/php/discussing-php-frameworks.html.


I've also entertained a similar idea, however my conclusions were not the best:

  • node.js seems to be much faster (+10x) than PHP in my benchmarks (simple loops)
  • implementing a PHP based web server can be done (and some projects already exist)
  • PHP isn't non-blocking by default (although it can be with streams and extensions like libevent)

My conclusion was: if you get libevent to work properlly with file handles / streams and code some wrappers around it you can have something that is very similar to node.js, just not in terms of performance. Also, one other important conclusion: when it comes to the garbage collector, PHP is not that great so it might not be a good idea to have a PHP script running forever as an HTTP deamon.


the biggest challenge i see is that node.js uses event based architecture of v8 engine, and php does not.


I've not tried it before, but you may try http://nanoweb.si.kz/

Also, other people have suggested that this will be built into php: https://wiki.php.net/rfc/builtinwebserver


PHP already comes with a command line interpreter (CLI), why would there be something else? Node.js cannot be something else, Javascript is not PHP; they do not share the same construct, they do not shsare the same paradigm, and Node.js fills the gap in having Javascript on the server side as well as on the client side... which gap does not exist with PHP.

But are you referring to having PHP listening to HTTP requests and all, like Node.js? In that caase, this will not work well like Node.js. You can still find some projects like what Arend's answer is suggesting, or using some other PHP classes/frameworks listening to HTTP connections and handle them... but the project will not pick up. IMO.


Thanks to everyone who have contributed in great depth to this topic :)

To sum up as I understand from all the above discussions:

1.) The lack of event driven behavior within PHP makes it a challenge to leverage it in an async. non-blocking IO pattern that is utilized within Node.js

2.) If it were possible to support events efficiently within PHP, the act of implementing something like a "Node.php" is more than a rip and replace of the engine, since much of the basic functionality is actually JavaScript written on top of the C code (such as http, console, log, net stream, etc.)

3.) The lack of first-class functions makes it a challenge to provide the same syntax and approach.

NOTE: I used the term syntax because I know there is a callback syntax within PHP and I would think that could be used instead of first-class functions but would not flow as elegantly.

4.) Performance characteristics of JavaScript v8 are proving substantially more performant than PHP.

Thanks again for all the great responses! My choice of using PHP at the time of prototyping was purely expediences and economics. If I had been made away of Node.js when the project was started I would have considered JS from the beginning. As I am now moving from prototype to production I must decide upon porting to something like Node.js, pure C++ or something like Erlang or Scala. A lot more questions and decisions ahead of me :P

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜