22

I was reading somewhere that sometimes PHP is simply not fast enough and that compiled code has to sometimes "do the heavy lifting"

What is the api in C++ to do this?

Subhanshuja
  • 390
  • 1
  • 3
  • 20
qodeninja
  • 10,946
  • 30
  • 98
  • 152
  • 3
    Before looking into mixing C++ with PHP, I'd say profile and make sure you find the section of PHP that's slow, and see if you could improve it in PHP first. Most interfaces that allows you to go from language X to C++ is going to look weird when you write the extension in C++. – Calyth Oct 01 '09 at 12:48
  • @Calyth, nice information, maybe it would be good one if move to question – Subhanshuja Nov 25 '19 at 09:37

4 Answers4

42

You can add functions/classes to PHP, programmed in C (and you can wrap a C++ class from C, if I remember correctly from an article I read some time ago), which might allow you to do some things faster -- if programmed well : no need for interpretation of PHP code ; only execution of machine code, which is generally way faster.

To do that, you'll have to develop a PHP extension.

There are not that many resources available on the Internet about that, but these one might help you to start :

And, specifically about the C++ part, this one might help too :

If you are really interested by the subject, and ready to spend some money on it, you could also buy the book Extending and Embedding PHP (some pages are available as preview on Google Books too) ; I've seen a couple of times that it was the book to read when interested on this subject (In fact, I've bought it some time ago, and it's an interesting read)

By the way, the author of that book is also the author of the first four articles I linked to ;-)

MMMTroy
  • 1,256
  • 1
  • 11
  • 20
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
11

You can actually execute compiled applications without any sort of API:

$output = exec('/path/to/yourapp');

Beyond that, you could always write a PHP extension. There's a good guide on the subject here: http://devzone.zend.com/article/1021

brianreavis
  • 11,562
  • 3
  • 43
  • 50
  • exec() is a big security vulnerability if an attacker passes a damaging command line to the function if escapeshellarg() or escapeshellcmd() is not used. – mosid Mar 09 '13 at 18:30
  • 19
    Of course it is. That doesn't make `exec` inherently bad, though—you just have to use it responsibly (like hard-coding the path). If you're worried about an attacker re-writing your scripts, you've got larger problems. – brianreavis Mar 15 '13 at 19:11
9

swig, the Simplified Wrapper and Interface Generator can help you wrapping (existing) c++ into a php module.

SWIG is a software development tool that connects programs written in C and C++ with a variety of high-level programming languages. SWIG is used with different types of languages including common scripting languages such as Perl, PHP, Python, Tcl and Ruby.
VolkerK
  • 95,432
  • 20
  • 163
  • 226
1

Well you have not defined what you are trying to do, but if you need to the C++ interface, then look at the ext directory in the source code to understand how to write a PHP extension that you can then load and use from your PHP scripts.

A couple of links that may help:

http://www.devarticles.com/c/a/Cplusplus/Developing-Custom-PHP-Extensions-Part-1/ http://devzone.zend.com/article/1021

Wayne
  • 3,415
  • 1
  • 22
  • 21