You are not logged in Log in Join
You are here: Home » Members » hewei » PHParser/PHPGateway » How to make include/require functions work in PHParser » howto_view

Log in
Name

Password

 

How to make include/require functions work in PHParser

 

Created by hewei . Last modified 2004-01-18 03:55:37.

This How-To gives you some tips on including other PHP scripts inside a PHParser object.

The PHParser/PHPGateway product provides a way to run PHP codes on Zope. For PHPGateway, all PHP scripts are staying in the file system. So virtually no script needs to be changed for running under Zope. But PHParser stores it's script in ZODB and if the script is not stand-alone and uses include/require functions, this How-To gives you some tips on making it work.

If you are going to use PHP's include() function to add "myfuncs.php" to a PHParser object like below, you have 3 methods:

    <?php

        include "myfuncs.php";
        ...
    ?>

  1. Remain myfuncs.php in the file system and add its path to the include_path or safe_mode_include_dir setting of php.ini. This method is suggested for including function/class libraries like PEAR .
  2. Upload myfuncs.php to Zope as a DTML Document (or File) object in a place where the following code can acquire. Note: Do not upload myfuncs.php as another PHParser, or you will get only the result, not the source, of the script included, which is most likely empty in this case:
        <?php
    
            // include "myfuncs.php";
        ?><dtml-var myfuncs.php><?php
            ...
        ?>
    
  3. Use the Zope File System product to synchronize myfuncs.php between Zope and the file system. Then you can use any of the above two methods.

But for include_once() function, method 2 is not enough. You need to have a mechanism to prevent classes or functions being redefined. For example, you can modify myfuncs.php to something like this:

    <?php // myfuncs.php
    if (!function_exists('my_func1')) {

        function my_func1(...)
        { ...
        }

        function my_func2(...)
        { ...
        }

        ...
    }
    ?>

The above instructions also work for require() and require_once(). To know the difference between include() and require(), click here.

Comment

Discussion icon chdir() then include() works well for me

Posted by: chrismiles at 2005-08-04

Rather than using include("/directory/path/to/my/php/scripts/myscript.php"); which fails when trying to include any other PHP files relative to it, I split this into chdir() then include() which works fine. Eg:

<?php
chdir("/directory/path/to/my/php/scripts");
include("myscript.php");
?>