Start compiling PHP extensions - php

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.

Related

How would I go about expanding my PHP code library with Python code?

I have a rather large API written in PHP that I've worked on for years. Some of the functionality needs to be upgraded to the extent where I should really just rewrite the classes. Since I need to scrap the old code anyway, I was thinking perhaps I could replace the old functionality with Python code.
I came across PiP while searching for answers, and it seems like an exellent solution (Since I can actually create Python class instances in PHP and call their methods etc.) but it seems it was abandoned in the Alpha stages for reasons unknown to me.
I suppose the simplest solution would be a CLI one, meaning I could run a python instance from PHP and collect the results. I don't particularly like this solution though, considering the amount of Python code I'd have to write just to handle input from PHP and respond accordingly. Plus it's not very reusable.
I don't know if this is a normal problem, Google certainly don't seem to think so, but what would be the best way of complementing a PHP code library with Python code?
I can think of two options, which should also make the overall solution somewhat more flexible:
Write it as a web service: Write the python parts as a RESTful web service. Should be relatively straightforward to do.
Use something like ZeroMQ to create a message queue: There are zmq libraries for both PHP and Python, which should also make this option not very difficult to implement.
As you may have noticed, all the options can be somewhat clunky in nature. You didn't mention what exactly did you mean by "API" (eg. is it a library or web service or what), so depending on what it is, it might just be easiest to rewrite it in PHP - it's not a bad language in the end if used properly.
After some time researching this, I've concluded that there is no worthwhile way of building on a PHP library with Python code. The PiP project was the closest thing I could find but it has been abandoned.
My solution has been to declare a 2.0. Every new function I add I write in Python. I translate some code from PHP to Python when necessary. It's working great so far!

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

How to develop C extensions for PHP apps?

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.

How to debug into internal c code of PHP?

Has anyone here tried it or is it possible?
I've been using PHP for quite a few years but never know exactly the underlying c scripts.
Is there a way to go into it?
I've done a bit of hacking on Zend PHP. I find it to be overly clever , some people go as far as calling it deliberately obfuscated in plain view. The source code to PHP is a mind altering (or breaking) substance, depending on how good you are at deciphering very cryptic macros. That's my impression of the core.
Writing extensions, however, is a breeze once you get the hang of the Zend helpers, most people with advanced-beginner / intermediate knowledge of C could get through a basic extension. There's also plenty of examples. One of the best parts of PHP is how organized the build system is, dropping in new stuff is relatively painless. With a little work and patience, almost any C library is rather easily extended to PHP.
If you aren't well versed in C (and what borders on abuse of the preprocessor), a glance at the PHP core is not going to give you much insight, nor is it a good thing to reference if you are learning C on your own.
Moving On:
Don't let anything I've said, or what anyone else has to say discourage you from grabbing the code and looking for yourself. That being said, as for debugging goes:
Valgrind (unless you use a lot of suppressions) is not very helpful. PHP (to the best of my knowledge) uses arch optimized reads, similar to new versions of glibc. I.e. its going to read 32 bits even if it only swallows 8 bits and a trailing NULL.
I have never found GDB to be very helpful with PHP. A lot of the magic is in macros that are very hard to trace.
You will quickly see the Zend error logging functions, and their version of assertions. Use those, printf() debugging is pretty much useless unless your debugging a CLI app.
Garbage collection can make you see odd things when using tools like valgrind's massif. Profiling heap use in PHP is an art I have not yet discovered.
Finally, I'd like to say its always nice to see someone take a look under the hood of their language. SO could use some questions that helps de-mystify the PHP core, so please feel free to post more as you go :)
Also, remember, Zend isn't the only forge that makes php. While compatibility with Zend is paramount if you hope for it to be adopted, everyone is still free to do their own thing.
I've never debugged the C code of PHP (nor extensions), but I've sometimes generated backtraces, in cases of crashes in PHP extensions.
This page might help, about that : Generating a gdb backtrace.
Starting from there, maybe you'll be able to go farther...
From some of your questions it sounds like you don't know that PHP is open-source, you can download the complete source code of it and look through all the C functions. If you want backtracing and debugging ability, you have to do what Pascal MARTIN said.

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