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"; ... ?>
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
chdir() then include() works well for me
Posted by: chrismiles at 2005-08-04Rather 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"); ?>