Inner workings of PHP - 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.

Related

Is phpDocumentor dead?

Or is it just at a 'finished' state? I've used PHPDoc for many years on all my PHP projects, but I recently noticed that the last post on the PHPDoc website was from 2008. So I'm wondering if it's time to look into other alternatives like Doxygen. Are there any advantages to using something other than PHPDoc?
EDIT: Interesting post on Dev Zone today when Matthew announced the release of Zend Framework 1.11.5 he wrote:
"Mike van Riel offered to convert our API documentation generation to DocBlox. We'd already been considering it for ZF2, but on seeing the flexibility of the templating system, and, more importantly for us in terms of packaging, the speed and minimal resources it utilizes in generating the output, we were sold. (API documentation generation time was reduced from taking 80-100 minutes to less than 10.) You can view the results for yourself." http://devzone.zend.com/article/13643
This is why I'm concerned, if large projects like Zend Framework are dropping phpDoc, it seems to me the inactivity of phpDoc is not going unnoticed. 100 minutes down to 10..that's what I like to hear.
#gms8994 good call
*UPDATE: So turns out DocBlox is PHPDocumentor2 in disguise/re-branded. http://www.docblox-project.org/
Although I currently use doxygen, too, I have to post that PHPDocumentor is not dead. Instead the attempts made by the DocBlox project are joined to form the basis for PHPDocumentor2 which sports a brand new website. These days I'm not convinced it's production ready but it does already look really promising.
I have recently used Doxygen for generating documentation for PHP.This is open-source tool for documentation and support other languages too.I would say this is a good tool and it easily generate documentation as well as class diagrams and have lots of configurable features.It is available for Windows as well as UNIX/LINUX
Can Find the latest release and DOWNLOAD Here
Why fix something that isn't broken? PHPDoc works great, doesn't need anything else really. They're not trying to innovate, just to help create documentation. Which they did, very well.
I had several problems with phpDocumentor. One of them was the xml export. After a few attempts to fix the code I decided to look for an alternative.
What I found and liked was: Rarangi
https://bitbucket.org/laurentj/rarangi/wiki/Home
Rarangi is a generator of documents from php source code.
The interesting thing about it was that it saves the information in a mysql db and you can make your own custom reports.
phpDocumentor v3 (with proper PHP 7 support) is currently alpha, getting real close to a stable release. The problem is that they don't have a lot of people working on it, so development is slow.
I've tested the latest alpha on one of my Symfony projects and it does the job okay, although it's missing some features and has a couple of bugs. They were all reported, of course.
https://github.com/phpDocumentor/phpDocumentor/releases
Update: phpDocumentor v3 just went beta. Check releases.

phpdoc vs. phpxref

Does one have capabilities the other doesn't? is it a problem the neither has been updated in about 3 years? Is there something lacking from both?
I like to think they target two different goals.
I wrote PHPXref over a decade ago as a quick hack to allow me to get to grips perusing the source code of a large project quickly & easily, without needing a lot of tools (just Perl, and not even that if you're on Windows) and without needing a remote web server. The documentation part of it was a useful side effect, but really I just wanted a decent way of reading through hyperlinked source code in a browser.
PHPDocumentor and similar tools do a much better job of generating real documentation from source in a variety of formats.
PHPXref could definitely use some updates (or a rewrite), but should still be useful today - You can download it and have output with no configuration in a couple of minutes, so it's cheap to see if it suits your needs.
The de-facto standard is PHPDocumentor.
A rather old comparison of phpdoc and phpxhref can be found in this short blog post:
http://kb.ucla.edu/articles/phpxref-vs-phpdocumentor
Since both tools haven't been update that much since then, it should still reflect the facts.
Another popular documentor is doxygen.
The new kid on the block is http://github.com/theseer/phpdox

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.

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.

Categories