Posts Tagged ‘MINIT’

Loading PHP Files in a PHP Extension

Saturday, March 8th, 2008

Just so I don’t forget again. Special thanks to Johannes S. on the PHP Internals list.

However, my problem is that I want to have the included PHP files with every PHP process, but only do that once. If the PHP files were in the include path, then they would still have to be included, but somehow I want the extension to include them and have them available.

First Method

The first method is not so much that it is impossible as it is impractical. Mostly when an extension is made, it is either a DLL or a SO file, or it is compiled into PHP. The PHP files can’t be included in the compilation, so they won’t make it in the DLL or SO or be compiled into PHP.

Therefore, it can be assumed in that case, that providing a relative link from C++ is impossible, because there is no way to know where the files will end up (unless you use PEAR, but even then it isn’t absolute). So you can’t just say.

PHP_MINIT(something)
{
    // Get file handle by using zend file handle struct

    zend_execute_scripts(...); // Run in a loop to include all files.
}

Unless you could get the PHP include path (which I believe you can, but I’m unsure how) and check each of those for the files. Then however, the reason you would use PEAR is to automate everything. Somehow, the PHP files how to end up in the PEAR directory or some directory that PHP has access to or is in the include path.

Second Method

The second method is using PEAR to automate the process of moving the PHP files, however if the files are in the include path, then there is no need to use zend_execute_scripts().

In this way, if the User didn’t know about the files, they wouldn’t have to try to include them. If they don’t try to include them, then my extension can do so and during the PHP_MINIT() process.

So the process then becomes:

  1. Build PEAR package
  2. Get PHP include Path
  3. In MINIT(), Search for files and if they exist loop through zend_execute_scripts()

Best Method

The best method would be to just rewrite the files as C/C++ which would increase performance better. However, it is in my opinion, that while that is the case, if you are depending on an external PHP library and want some performance, and want to only compile the scripts once per process (if the process is kept running for a long time), which might at least keep the library in memory for each PHP run after the first providing at least some advantage. If you don’t want to keep playing catchup with the external library, then the second method will work best.

If playing catchup every time a new release is made is no issue because of performance, then converting the PHP to C/C++ would work best. I’m of the mind, that using both methods to write the most used in C/C++ and then having the rest in PHP files is better.

I’m not going to write any code at this time.

Possibly Related Posts: