Hacker News new | ask | show | jobs
by DonnyV 5292 days ago
I'v been thinking about using Php lately. I come from a C# background, so I was poking around to see what I would be missing or gaining from Php. Why haven't they added a compiled option yet? I know you can use http://phpcompiler.org compiler to do it but why hasn't it just been added to the project? The speed increase is well worth it. Also I noticed Php doesn't have generics or Template classes. I use these a lot in my C# libraries. Anyone know if thats going to be added anytime soon? I do like that its used everywhere. Also looking for a good MVC framework for Php like Asp.net MVC.

UPDATE Can you please stop down voting. I'm not hating on Php, read my other comments below.

4 comments

Didn't downvote you, but

1) You're hijacking the thread with something unrelated and better suited for some online research or sites like SO

2) According to your questions you're not investigating PHP, you're looking for C# in disguise/in another set of clothes.

Yeah your probably right about asking this here. I was hoping for some Php experts maybe to point me in the right direction. I'm not trying to hate on Php just want to learn an open source framework or language that has similar features to C#. I really don't want to learn Java because I think thats a dead language. I'd rather use Mono but they don't have very good support for asp.net mvc.
Have a look at some jvm-based languages/frameworks. Grails/Groovy, and Rails with JRuby may be interesting to you. Both Groovy and JRuby have strong metaprogramming aspects, which may be of interest.

Heck, even Play! might change your mind about Java.

In the PHP world, there's a huge number of choices/options out there, and finding something that fits your style can be a challenge (because, until you know your style, you don't know what fits).

http://www.reddit.com/r/php would probably be a good place to ask these questions too.

Check out HipHop which transforms PHP into C++ then compiles that using G++ https://github.com/facebook/hiphop-php#readme This is what Facebook uses.

That aside, PHP is plenty fast as long as you avoid Zend and design your apps correctly for what you're doing. Lots of bad stuff out there giving PHP a bad name, like Magento.

I too wish php had a widely supported compiler, but in reality it's not something that most projects require. The overhead of interpreting php is a lot lower than e.g. making a single db call. Also, in the most common usage scenarios, PHP processes only live for the time it takes to deal with each request, so how do you approach that if you're compiling your code? Keep spinning up new processes or rewrite your application code?

Regarding MVC frameworks, try Yii - http://www.yiiframework.com

I wouldn't dismiss PHP's speed problems that quickly. Opcode caches typically don't speed up the actual execution, just the loading and parsing. I use zend server (with zend optimizer) and I've got PHP code that is hard CPU bound, as optimized as it can be, and still takes minutes to run to process perhaps 2 megabytes of raw data. By contrast, the exact same feature implemented in Delphi runs in less than 2 seconds, even though the code is an order of magnitude less efficient.

PHP isn't just orders of magnitude slower than JIT-ed or native compiled languages, but it has no affordances for that fact. There's no standard way to run code in the background. Zend won't start working on one either, because they sell a jobs queue as part of their Zend Server product.

Frankly, a JIT-ed PHP is the only feature I need. I appreciate the syntactic sugar added in the last few revisions of PHP, but honestly, I'd trade every new feature since PHP 5.2 for a bit more speed. With stuff like closures and traits it seems like they're working on the easy stuff instead of the necessary (but hard) stuff.

These speed charts are why I would compile Php. http://naspinski.net/post/AspNet-vs-php--speed-comparison.as... It seems worth it too me.

Thanks for the Yii link.

those charts don't say whether APC is enabled, which leads me to suspect that it isn't. APC is an opcode cache that greatly enhances php performance, no one runs a high traffic website without using APC
eAccelerator and XCache also do the same(ish) thing. APC usually helps way more on subsequent requests. While APC /can/ help a lot, it's not the magic speed bullet that some make it out to be -- if your app is a bodged mess of Joomla and stuff that you got outside developers to write for you, it'll probably still run like garbage with APC enabled.

Getting a bit off topic here, but one of the biggest performance killers I've seen is PHP devs writing Really Awful SQL(tm). Even beyond stuff like failing to use PDO and prepared queries to avoid SQL injections, if you look around you'll find tons of just really abysmal SQL. Typical problem: someone is using an oper in SQL that forces MySQL to not use indices. This absolutely kills performance and you typically won't notice it as a developer unless you're pre-populating your database with a large "real-world" dataset and doing proper performance testing.

Did not know about APC...nice. Does APC just compile it after the first time and save it in memory? Can you use APC to create a packaged compiled file?
bytecode caches (like APC) store the bytecode on the fly in memory. you can't create somthing like a "binary" file.

edit: at least they're not intended to create "binaries". you can probably mess around with APC and make php look and behave like a compiled language. but you won't gain any notable speed improvements beyond the on the fly caching.

edit2: you can store php projects in a jar like file, caller phar. http://php.net/phar

While it's a good idea to look around what language A could borrow from language B once in a while, it's important to not fall into the trap of imitating B at all costs in A.

In this specific case, I don't think it's a good idea to do C#/ASP.Net in PHP and vice versa. Most PHP hosting environments do have a compile-once-run-many-times paradigm by virtue of opcode caching. The advantage over a typical Java or .NET app server is that you can change files and they get automatically recompiled on the fly. So for all intents and purposes, PHP is usually running a piece of in-memory compiled bytecode.

There are many MVC frameworks for PHP, for any interpretation of "MVC" you might want. I'm sure you can find one that's closely modeled after ASP.net, but again I'm not sure this is a productive undertaking in principle.

Although I don't use it I have heard the symfony2 is one of the better MVC frameworks out there. The same people have also made quite a nice templating language called twig that you may also want to look at (http://twig.sensiolabs.org/).