views:

70

answers:

3

I know you can technically make PHP extension just by making a PHP file and using require_once.

But would it optimize the performance if you wrote an extension in C or C++.

If so, how would you make a "hello-world" for that?

+2  A: 

You can incorporate a stand-alone C/C++ program into your PHP site simply by executing it and passing the output to the browser. As far as truly inter-operating, it's not worth your time to write a library containing site-specific functionality. This simply isn't done except to interface with other existing libraries (MySQL, imagemagick, etc).

Programming for the web isn't like application development. Optimizing bits of your code by writing them in "faster" languages (analogous to writing small, tight looping bits of your C++ program in assembly) isn't worth the development time. You're better off keeping your application easily maintainable, this is why scripting languages dominate the web.

meagar
+1  A: 

Here's a tutorial on PHP extensions. Whether it will optimize the performance or not, it depends on what you are trying to wrap on your extension. But I would not write a PHP extension just for optimization purposes. I would write one if I have no choice. I.E. Wrapping a common C library to make it available directly in PHP...

Pablo Santa Cruz
+2  A: 

Software written in C/C++ certainly does run faster than code in PHP. And you can write an extension in C/C++ and link it into PHP. The PHP manual covers this here: http://php.net/manual/en/internals2.php

The other answers give links to other tutorials for writing PHP extensions, and you can google for "PHP extension tutorial" to find more.

But whether this is the right thing to do in your app is another story. Most experts agree that PHP runs just fine, fast enough for 98% of applications. The instances where PHP isn't fast enough are not in general due to the language, but an inefficient application architecture that the programmer has created. That's a weakness that can't be remedied by rewriting parts of your app in C/C++.

Bill Karwin