How does PHP main.c work?

I bumped into the source tree of PHP 5.3.1 and decided to look at main.c. I was curious what happens behind the scenes whenever PHP is running.

I got the impression that any C or C ++ program starts execution in a function named main, but I do not see a function with this name in main.c.

Where does the PHP code really start to run (different for the command line compared to MOD_PHP and CGI?), And that I miss w / r / t any main function in the main.c file that would allow me to answer this question myself next time ?

+4
source share
3 answers

I don’t think I have ever seen any clear answer to this question on the Internet, but you might be interested in some paragraphs of the PHP Extension and Implementation book, which is probably a reference when it comes to writing PHP extensions and internal components PHP engine.

An interesting couple of sentences citing Chapter 1, The PHP Life Cycle:

In a common web server environment, you will never explicitly run a PHP translator; you will start Apache or some other web server that will load PHP and process scripts as needed ...

And, right after:

... the CLI binary really behaves the same way. The php command, entered in the system prompt, is launched by the "command line API", which acts as a mini-web server, designed to serve one request.

You can probably find several pages on Google books if you want to try reading a little more ...

+2
source

The main() function does not have to be in the main.c. file For the command line interface, php main() is located in php_cli.c (line 642).

+3
source

Usually for the "main" is the entry point to C / C ++, and standards relate to it specifically because of this, but this is not the only possibility (it is the only one required by the standard, however). How this is actually handled depends on the implementation, since the runtime library needs to tweak the settings before your application takes control. See linker settings for a definitive answer.

php_module_startup looks like it could be what you want, it could be what is ultimately called from a real entry point.

+2
source

All Articles