Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use C++ code to interact with PHP? [closed]

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?

like image 369
qodeninja Avatar asked Oct 01 '09 06:10

qodeninja


People also ask

Can we use C++ in PHP?

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.

How do I communicate between C++ and PHP?

1) To take something from C++ app PHP connects to that app using stream_socket_client function. C++ app is listening on host and port using socket , bind and listen functions. As soon as connection arrives C++ app accept it and then in a separate std::thread serves it ( recv to get request, send to send response).

Is PHP built on C?

PHP is written in C, not C++. Why don't you take a peek in the PHP source code and see for yourself? no it does not translate anything into c++, although facebook developed a compiler that does translate PHP into C code, and then into machine language, called hiphop or someting like that.


3 Answers

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 :

  • Extension Writing Part I: Introduction to PHP and Zend
  • Extension Writing Part II: Parameters, Arrays, and ZVALs
  • Extension Writing Part III: Resources

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

  • Wrapping C++ Classes in a PHP Extension

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 ;-)

like image 116
Pascal MARTIN Avatar answered Sep 20 '22 14:09

Pascal MARTIN


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

like image 33
brianreavis Avatar answered Sep 22 '22 14:09

brianreavis


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.
like image 30
VolkerK Avatar answered Sep 19 '22 14:09

VolkerK