Knowing a little more about PHP

Hello dude! welcome back to ThyPHP, your PHP blog!. Today I will write about how to compile an extension of PHP on Unix and about an interesting tip found on PHP creator blog.

Yesterday I saw a post in Rasmus Lerdorf “toys page”. It was a nice post about MVC model… but the part that I surprise me…

Try to avoid using include_once and require_once if possible. You are much better off using a straight include or require call, because the *_once() calls are very slow under an opcode cache. Sometimes there is no way around using these calls, but recognize that each one costs you an extra open() syscall and hash look up.

This have some sense (he’s a main PHP code developer :-) ), so I was figuring out how to solve this problem. The idea that I have was to have the C-like solution, use the “#ifdef”. Look in the example:

<?php
if ( ! defined(’SCRIPT_NAME’) ) {
define(’SCRIPT_NAME’, true);
//the code it self
}
?>

You can include the above PHP script. But after see this code again, it will be same thing that *_once…, because it will call open(), compile the code, and then execute, and avoid all execution because the first if will be false when a file included more than one time.

The only solution that I can figure out ( If you have other, please let me know) is to call the following function instead of call *_once:

<?php
function safe_include($script) {
static $included;
if ( ! isset($included[$script]) ) {
include $script;
$included[$script]=1;
}
}

?>

It is a very trivial and simple solution, but it may work. Of that the better solution is to code it well… ;-)

Compiling an extension for PHP

Some times when you are so lazy (such as me) for download and install PHP from it source (I love yum!) you may find that you need an extension. So the solution is to install PHP from the source?. The answer is not! You will need to compile somethings, but not install it…

  1. Download the PHP source tarball.
  2. Uncompress it (tar xfvz file-name.tar.gz)
  3. Compile it.
    • cd php_dir
    • ./configure –with-dba=shared –with-sqlite=shared
    • make
  4. Copy modules files to PHP extensions dir:
    • modules to copy = find . | grep module | grep so$
    • php extension dir = grep extension_dir /etc/php.ini
  5. Back in PHP source code dir execute make distclean for clean the source code.
  6. You can delete the the source folder now.
  7. Create a file in /etc/php.d/module.ini (for each module) with the following content:
    extension=$module.so ; $module is your module name!
  8. Restart your websever
  9. It is done

8 Comments

  1. Comment by fluminis on March 6, 2008 11:23 am

    if ( defined(’SCRIPT_NAME’) ) {

    seem’s that you won’t enter into the if.
    maybe you think :
    if ( ! defined(’SCRIPT_NAME’) ) {

    nice post

  2. Comment by Cesar D. Rodas on March 6, 2008 2:48 pm

    Yes, you’re right fluminis!

    thanks for the “bug” report

  3. Comment by Anti Veeranna on March 6, 2008 3:52 pm

    Why not use autoload?

    http://www.php.net/autoload

  4. Comment by DH on March 6, 2008 4:44 pm

    “Premature optimization is the root of all evil.”

    I wouldn’t do anything to avoid require_once() without some solid profiling data (e.g. XDebug) to show it would make an important difference.

    And if you have a huge codebase where only small parts are active at a time, perhaps __autoload() would be more useful.

  5. Comment by kenman on March 7, 2008 12:39 am

    Have you tested safe_include() much? I’m guessing it would fail for some cases in which a file with the same name is included, but from different directories (I know, this is typically poor form, but it sometimes happens).

    I think this would be safer for situations such as that:

    function safe_include($script) {
    static $included;
    $script = realpath($script);
    if ( ! isset($included[$script]) ) {
    include $script;
    $included[$script]=1;
    }
    }

  6. Comment by kenman on March 7, 2008 12:41 am

    Actually, that probably wouldn’t be much better. I think you’d be forced to change calling of the function itself:

    safe_include( realpath(’foo.php’) );

  7. Comment by Eli on April 30, 2008 2:40 pm

    Rasmus lead dev?? really?? not since php left perl.

  8. Comment by odxf qzonsya on September 14, 2008 8:37 pm

    rvzj eigry fmqidy kleswbvo unrcb uoghakvx anef

Comments RSS

Leave a comment