How to develop C extensions for PHP apps? - php

My PHP app has a number-crunching part that is just to slow for PHP, so I was thinking of building a custom C extension, but it is impossible to find any good reference to start with :(
Is there a guide on how to do something like this?

The best resource, although outdated in several aspects (it only covers PHP until version 5.1) is Extending and Embedding PHP by Sara Golemon. Even more outdated is the PHP documentation. On the other hand, the content on the PHP wiki is very up-to-date, but also quite incomplete and not very oriented for beginners. See also these articles, part V of Advanced PHP Programming by George Schlossnagle, chapter 14 of Programming PHP by Rasmus Lerdorf and Kevin Tatroe and, specially, these slides.
Finally, the most authoritative source you'll find is the source code of the extensions bundled with PHP.

This might not be an answer but more like a suggestion, There are tools out there to compile your php to an executable, which you could just then use as an extension. This Would uniform your code a bit and unify your project. I have tried something like this a while ago. The compiled php acts no differently than the compiled c would.

Another option would be a command line tool written in C, that you start from PHP and communicate with over stdin/stdout. In many cases this is much easier to write and to deploy, but it ultimately depends on your use case.

Related

What exactly is HHVM and/or PHP execution engines?

I've been reading about Facebook's Hack which lead me to reading about HipHop Virtual Machine. I wanted a better understanding of this and could not find a clear definition. Wikipedia defines it as: HipHop for PHP (shortened as HipHop) describes a series of PHP execution engines and improvements created by Facebook. I don't understand what PHP engines are...
So I guess then my question is: What exactly are "PHP execution engines?" and how exactly do the benefit PHP applications?
Thanks!
You can run your PHP code with the standard (Zend) engine that you download from php.net, that works great. If you download WAMP, MAMP, or any of the other pre-packaged PHP & MySQL for your Operating system packages, this is what you're getting.
Alternately you can run (most of[1]) your PHP code with HipHop, that works great.
Which one you're using should be effectively invisible to your end users. Your developers, and operations teams will need to know.
You may choose to use HipHop if you're running a site that gets a lot of traffic. HipHop while not supporting all of the features of PHP, does run a bunch faster. It also has some new features not available in the main PHP distribution mostly to do with type hinting. These can provide some pretty attractive tools to read through your code checking for bugs.
Reasons you may want to use HipHop:
Performance is a big deal for you
The static analysis tools available with HipHop have serious worth to your team
The new features in HipHop but not in Zend PHP are attractive to your team.
Reasons you may want to use Zend PHP
You need an extension not available for HipHop (there's lots of extensions out there, think: gd, curl, imagemagik, etc. Many are available for HipHop now, many aren't).
Your code, or framework is making use of unsupported features.
You have a lot of expertise in hosting your current Webserver & PHP stack, and don't want to start from scratch.
This post goes into some HHVM vs HACK differences, and gives a nice run down: http://www.marco.org/2014/03/21/hack
[1] Not all code that you can run with the regular PHP engine currently works on HipHop. They're working on most of the issues, some they've just decided not to fix (I think variable variables may be an example of this e.g. $$var)

compiling PHP 5 to EXE?

i have been using the awesome bambalam: http://www.bambalam.se/bamcompile/ compiler to make exe from php source.
But it's getting old, and it doesn't support php5.
Is there another project that will do the trick?
Thanks
ADD1:
i need only for windows
Please note, this was answered before the OP added the vitally important "I need only for windows" addition to the question.
Well the way Facebook handle this is by compiling to an intermediary language, C++, via a piece of software called HipHop for PHP.
There's also PHC, and a few others. Zend Server caches the bytecode which helps a lot.
You can try exe output, get it here, www.exeoutput.com. It has amazing features supports html, flash, javascript, php, etc.
Give a try to one of these tools:
PHPFlexer, if you only have a single php file (like a quick commandline tool)
RapidEXE, if it's several files/folders; here you can even choose a PHP version
PHC-Win, it's the ancestor of PHPFlexer and works pretty well
ExeOutput, a big monster but it gives you a webserver environment and a nice GUI
ZZEE, despite its silly name it's pretty good but features some oldish PHP
The first 3 is command-line, the last 2 are able to develop a window-based application; but they're slow, sometimes a bit tricky to configure and they also make a big fat exe file that loads relatively slow (especially ExeOutput). Command line ones are pretty compact and easy to use, with an output exe around 2MB.

What's a good non-web application development environment for someone comfortable with PHP/Javascript/Mysql?

I would like to be able to write database driven applications (i.e. standalone apps that are not web based and don't require a browser or Apache server to run)
I have attempted to do this in Codegear C++ Builder in the past but even though my 'background' is in that (C++ OOP with Borland Builder) it is so far removed from doing the same sort of thing with PHP/mysql and other web technologies that I found that I couldn't get very far for a lot of effort getting it to work. It was a while ago now but I was using the built-in database engine that comes with Builder and I just found it frustrating and difficult.
In other words - Is there something out there that will allow me to make use of web based languages to write standalone applications (specifically PHP/Javascript/mysql)
You can stick with PHP if you want. There are QT bindings, GTK bindings, OSX/Cocoa bindings, and you can call Win32 functions. I don't know how stable all of those are, but you can do GUI in PHP as well as the command line stuff.
As for other languages... PHP is very C like. It started as basically a scripting wrapper around C (IIRC), which is why you have functions named after the C standard library (like strstr). C like languages will feel quite familiar.
I would think Python would probably be the closest to PHP. It's a scripting language, syntax is somewhat close, it has a ton of libraries, and is very well supported and commonly used. I'd imagine it would feel pretty familiar. Using indenting instead of brackets for blocks can throw some people, but it fit the way I already indented my code.
Ruby is quite popular, and is also a scripting language. I think it's farther away, syntax wise, than Python, but I've never really used it so I can't promise that. I know it has at least GTK bindings.
Perl has a lot of resources and bindings, but isn't as easy to read as PHP since you have to learn the special variables like #_. It was never really my cup of tea.
You do have the C/C++ stuff, and Java has it's large library. You may want to go that way since you say you've used C++ before. If you're on a Mac (or willing to use GNUStep) you could go with Objective-C/Cocoa. That's getting rather far from PHP syntax though.
All of these languages have database connections. You don't mention what platform you're on.
But for easy to work with, quick to pick up, works all sorts of places, and can definitely do GUIs... Python would be a good choice to look at.
You could always use PHP. It is a decent command-line / programing application. Other than that all I can say is that you knowledge of database access and storage will be helpful but by the end of the day you are going to need to learn a new language.
Most languages have libraries for database access. Just pick one that you like the feel of. It is also a good idea to choose one that is popular (for the community support) and free libraries are always nice. Also look for good documentation and one that is fairly standard.
A nice thing to know is that javascript and php syntax are very similar to many other languages. (Javascript looks almost identical to C and C++). Just read the main language tutorial then the database API tutorial and you should be good to go

Start compiling PHP extensions

I have code that is straight forward, but intensive. I would like to compile it into a PHP extension. What is the simplest / best practice for going about that?
I have a Java background, so writing the C / C++ code shouldn't be an issue. I would, however, like to avoid any pitfalls along the way.
A simple tutorial or walk through is something I haven't been able to find. Plenty of examples, but nothing aimed at someone new to doing this.
It looks like a more arduous road then I though, especially with all the lovely C quirks.
The PHP Manual and this Zend article seem to cover quite a bit of ground in regards to writing PHP extensions.
The the tag info for php-extension and in particular, this answer, also linked from there.
You should try SWIG for generating wrapper for your source code, and then create your extension. It supports various languages like Java, C/C++ and makes it as simple as possible.

Inner workings of PHP

I was wondering if there's a decent resource for finding the inner workings of PHP.
I have taken a look at the source, but a decent explanation would really help.
Some example questions I'd like answered.
How does a PHP script get interpreted into machine readable code?
How does it interact with Apache, how does apache collect the HTML response from PHP?
And other questions like that.
It may be a bit outdated but I think you'll find this useful:
PHP internals and the Zend API
Have you taken a look at this thread on Reddit:
IAm finishing my PhD in compilers. I wrote a PHP compiler. AMA
http://www.reddit.com/r/IAmA/comments/9rpa3/for_my_fellow_geeks_iam_finishing_my_phd_in/
including the related Google Tech Talk.
The best book on te topic is Sara Golemon's Extending and Embedding PHP.
It uses PHP 5.1.0, but everything should be appliable to any 5.x. You can find information about changes to 5.3 (especially for namespaces) in the source code.
The PHP website is http://php.net/. It has a lot of information.

Categories