note 56817 added to function.set-include-path
| From: | kencomer at NOSPAM dot kencomer dot com | Date: | Thu, 15 Sep 2005 21:24:52 +0000 |
| Subject: | note 56817 added to function.set-include-path | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-95249@lists.php.net to get a copy of this message | ||
A handy-dandy function to add to the include path while eliminating duplicates (two versions) with a
testbed so you can check it on your machine:
<head></head>
<body>
<?php
/**
* Add a directory to the php include path without adding a duplicate
*
* <p>
* </p>
* <p>This is a file from
* </p>
* @author Ken Comer <kencomer@kencomer.com>
* @package
* @subpackage
* @version
* @copyright was © 2005 Ken Comer, released to public domain by author
*/
/**
* short version, no intermediate storage
*
* @author Ken Comer <kencomer@kencomer.com>
*/
// does not use get_include_path so as to be compatible with PHP4
if (!defined('SEP')) define('SEP',PATH_SEPARATOR); // PHP Magic Constant
function add_include_path($paths,$sep=SEP) {
ini_set(
'include_path',
implode(
$sep
,array_flip(array_flip( // flipping twice removes duplicates
array_merge(
explode(
$sep
,$ini_get('include_path')
)
,explode(
$sep
,$paths
)
)
))
)
);
}
/**
* vanilla version with intermediate storage and documentation
*
* @author Ken Comer <kencomer@kencomer.com>
*/
// does not use get_include_path so as to be compatible with PHP4
function add_include_path_long($paths,$sep=SEP) {
// following lines transform '/path1;path2'
// into array( 1=>'/path1', 2=>'/path2' )
$ini_paths = explode($sep,ini_get('include_path')) ;
$passed_paths = explode($sep,$paths);
// combines array( 1=>'/path1', 2=>'/path2' )
// and array( 1=>'/path2', 2=>'/path3' )
// into array( 1=>'/path1', 2=>'/path2',
3=>'/path2', 4=>'/path3' )
$combined_paths = array_merge($ini_paths,$passed_paths);
// flipping once makes the paths into the keys,
// duplicate keys are eliminated automatically
// array( 1=>'/path1', 2=>'/path2', 3=>'/path2',
4=>'/path3' )
// becomes array('/path1'=>1, '/path2'=>2,
'/path3'=>3 )
// flipping again returns the array back
// into array( 1=>'/path1', 2=>'/path2', 3=>'/path3'
)
$combined_paths = array_flip(array_flip( $combined_paths ));
// turn array( 1=>'/path1', 2=>'/path2', 3=>'/path3' )
// into '/path1;/path2;/path3'
$ini_paths = implode($sep,$combined_paths);
ini_set('include_path',$ini_paths );
}
?>
<?php
// verification testbed
echo "include_path=".get_include_path();
add_include_path_long('./include');
echo "; after adding ./include =".get_include_path()."\n";
?>
</body>
----
Manual Page -- http://www.php.net/manual/en/function.set-include-path.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+56817
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56817&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56817&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56817&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56817&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56817&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+56817&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56817&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+56817&report=yes
Search -- http://master.php.net/manage/user-notes.php