I am creating an autoloader that extends include_path. It takes an array, adds the explode () d include path, removes all links to the current directory, adds one current directory at the beginning of the array, and finally appends () it all together to form a new include path. The code is shown below
<?php static public function extendIncludePath (array $paths) { // Build a list of the current and new paths $pathList = array_merge (explode (PATH_SEPARATOR, $paths), explode (PATH_SEPARATOR, get_include_path ())); // Remove any references to the current directory from the path list while ($key = array_search ('.', $pathList)) { unset ($pathList [$key]); } // Put a current directory reference to the front of the path array_unshift ($pathList, '.'); // Generate the new path list $newPath = implode (PATH_SEPARATOR, $pathList); if ($oldPath = set_include_path ($newPath)) { self::$oldPaths [] = $oldPath; } return ($oldPath); } ?>
I would also like to use array_unique () in the array before using it so that PHP does not continue to search for the same place several times if someone is careless and points to the same path more than once. However, I also need to maintain the sort order of the array, because include looks in directories in the order in which they are defined in the include path. First I want to look in the current directory, then the list of search directories, and finally, the original include path, so that, for example, the old version of the shared library by default include_path is not included in favor of the newer version in my search list.
For these reasons, I cannot use array_unique () because it sorts the contents of the array.
Is there a way to get array_unique to keep the order of the elements in my array?
Gordonm
source share