tags:

views:

106

answers:

3

I was poking around the PHP 5.3.1 source tree, and decided to take a look at main.c. I was curious what was happening behind the scenes whenever PHP runs.

I was under the impression that any C or C++ program starts execution in a function named main, but I don't see a function with that name in main.c.

Where does PHP code actually start executing (a different for command-line vs. MOD_PHP vs. CGI?), and what am I missing w/r/t no main function in the main.c file that would let me answer this question myself the next time?

+1  A: 

I don't think I've ever seen any clear answer to that kind of question on the Internet, but you might be interested by some paragraphs of the book Extending and Embedding PHP, which is probably the reference book when it comes to writting PHP extensions, and the internals of the PHP engine.

An interesting couple of sentences, quoting chapter 1 "The PHP Life Cycle", is :

In a common webserver environment, you'll never explicitly start the PHP interpreter ; you'll start Apache or some other web server that will load PHP and process scripts as needed...

And, just after :

... the CLI binary actually behaves just the same way. A php command, entered at the system prompt, starts up the "command line API", which acts as a mini-web server designed to service a single request.

You'll probably be able to find some pages on Google books, if you want to try reading a bit more...

Pascal MARTIN
Or find the book in Safari and, you know, read it :) Thanks! Very interesting reading.
Alan Storm
@Alan : I actually have the book, and have read it -- that's why I pointed to it ^^ (never checked about the safari stuff, though ; should try it, one day...)
Pascal MARTIN
+2  A: 

It's common for 'main' to be the entry point in C/C++, and the standards treat it specially because of that, but it's not the only possibility (it is the only one required by the standard, however). How it's actually handled is implementation-specific, since the runtime library needs to set things up before your application gets control. Look at the linker settings for the final answer.

php_module_startup looks like it might be what you want, it could be what's eventually called from the real entry point.

Roger Pate
+3  A: 

The main() function doesn't have to be in a file called main.c. For the php command line interface main() is in php_cli.c (line 642).

sth
That may only be the case when PHP is invoked via the commandline. I wouldn't be surprised if the entry point is different when, for example, PHP is invoked via mod_php.
Frank Farmer
Sure, mod_php will be loaded by the webserver as a shared object/DLL/... and then the webserver will call whatever functions it wants to call in that module. But then php doesn't run as a program on it's own, it runs as part of the webserver.
sth