Alright, it looks good in concept, but the function is more of an interface. I'm trying to set it up so you can just call the library, independant of what's acually IN the library, and then reference that library's variables directly from another script, or even another library. Here's my "libs/code.php":
<?php
# Core Function Library
# Copyright 2003 Zallus Kanite.
function getlib ($lib, $version = NULL)
{
foreach ($GLOBALS as $key => $value)
{
global $$key;
}
if (empty($library[$lib]))
{
if (is_string($version))
{
$library_name = $lib;
$library_version = $version;
}
else if (is_int($version) || is_float($version))
{
$dh = opendir(LIB_DIR);
while (false !== ($filename = readdir($dh)))
{
$filelist[] = $filename;
}
rsort($filelist); reset($filelist);
foreach ($filelist as $filename)
{
if (eregi($library . '-' . $version, $filename))
{
$filename = explode('-', $filename);
$library_name = $filename[0];
$library_version = $filename[1];
break;
}
}
}
else if ($version == NULL)
{
$dh = opendir(LIB_DIR);
while (false !== ($filename = readdir($dh)))
{
$filelist[] = $filename;
}
rsort($filelist); reset($filelist);
foreach ($filelist as $filename)
{
if (eregi($lib, $filename))
{
$filename = explode('-', $filename);
$library_name = $filename[0];
$library_version = $filename[1];
break;
}
}
}
$library[$lib] = array('name' => $library_name, 'version' => $library_version, 'path' => LIB_DIR . '/' . $library_name . '-' . $library_version);
return require_once (LIB_DIR . '/' . $library_name . '-' . $library_version . '/code.php');
}
else
{
return false;
}
}
function verlib ($lib)
{
global $library;
if (!empty($library[$lib]))
{
return $lib . ': ' . $library[$lib]['name'] . '-' . $library[$lib]['version'] . ' active.';
}
else
{
return $lib . ': inactive.';
}
}
?>
That's what I've got right now, but it unloads any variables not expected in the function scope. I could do the C thing and prototype any variables, like so:
// In 'main.php';
$sql = array();
$foo = array(); # I like it better than NULL, which doesn't detect.
$bar = array();
getlib('sql', 1.21);
echo $sql . $foo . $bar;
// In 'sql-1.21/code.php';
getlib('foobar', '1.00');
$sql = 'YAY';
// In 'foobar-1.00/code.php';
$foo = 2;
$bar = $sql;
In theory, that'd look nice, but I don't want the people who write one library relying on a constant interface from another library. Maybe another script called 'declare.php' in the directory of the script, that declares a bunch of $GLOBALS['foo'] = array();... If I could avoid it however, a solution that just modifies getlib() would be appreciated.