LiraNuna's Development Blog
PHP Path Resolution – Now PHP 5.3 compatible!
Posted on Sunday 29 August 2010

Presenting the PHP 5.3 compatible version of my PHP path resolution class!

Why there should be a difference? Because my original version used create_function, which… created a whole new function object every time you used one of the class’s methods. This was very memory expensive and even incurred a slight performance hit.

The new version uses PHP 5.3’s anonymous functions to create more readable code in addition to reducing memory consumption and execution time.

As usual, the source code is under the WTFPL for you to enjoy without any restrictions.

 * @class Path
 * @brief Utility class that handles file and directory pathes
 * This class handles basic important operations done to file system paths.
 * It safely renders relative pathes and removes all ambiguity from a relative path.
 * @author Liran Nuna
final class Path
	 * Returns the parent path of this path.
	 * "/path/to/directory" will return "/path/to"
	 * @arg $path	The path to retrieve the parent path from
	public static function dirname($path) {
		return dirname(self::normalize($path));
	 * Returns the last item on the path.
	 * "/path/to/directory" will return "directory"
	 * @arg $path	The path to retrieve the base from
	public static function basename($path) {
		return basename(self::normalize($path));
	 * Normalizes the path for safe usage
	 * This function does several operations to the given path:
	 *   * Removes unnecessary slashes (///path//to/////directory////)
	 *   * Removes current directory references (/path/././to/./directory/./././)
	 *   * Renders relative pathes (/path/from/../to/somewhere/in/../../directory)
	 * @arg $path	The path to normalize
	public static function normalize($path) {
		return array_reduce(explode('/', $path), function($a, $b) {
			if($a === 0)
				$a = "/";
			if($b === "" || $b === ".")
				return $a;
			if($b === "..")
				return dirname($a);
			return preg_replace("/\/+/", "/", "$a/$b");
		}, 0);
	 * Combines a list of pathes to one safe path
	 * @arg $root	The path or array with values to combine into a single path
	 * @arg ...		Relative pathes to root or arrays
	 * @note		This function works with multi-dimentional arrays recursively.
	public static function combine($root, $rel1) {
		$arguments = func_get_args();
		return self::normalize(array_reduce($arguments, function($a,$b) {
				$a = array_reduce($a, 'Path::combine');
				$b = array_reduce($b, 'Path::combine');
			return "$a/$b";
	 * Empty, private constructor, to prevent instantiation
	private function __construct() {
		// Prevents instantiation

1 Comment for 'PHP Path Resolution – Now PHP 5.3 compatible!'

    November 12, 2015 | 14:18

    out paychecks to all the students.Nobody handed a check to my pal.When he or she asked where his pay back was, one instructor pointed towards the very top of the pole not to mention said “I nailed this up certainly, there.If you need to get compensated, you? ll need to climb up and obtain it.? Evidentially, the need to buy food for her wife not to mention two kids was much better than his fear of heights.He retrieved his check and continued to graduate student pole hiking school not having further occurrence.
    Can there be some concern that? s keeping you on the floor? Is there some achievement that can be found just beyond your reach since you? re as well afraid to climb higher enough to achieve it? If that’s the case, you need to take action.
    Concern, and it’s two companions? uncertainty as well as doubt, are powerful psychological causes that hold a lot of us back via being what we should are designed to be.However, like other things that resides solely within our own memories, we possess the power so that you can overcome such forces.
    All it requires is the conscious decision to face our fears head on and have a look at what it is that we’re really worried of.Once we understand the underlying reason behind the worry about, we can develop a plan in order to combat them.Here? s the way you do RD6}FY$\\\\REERD1\\\\\\REERD1\Y}Y2\\\\\\\\\\\\\\\\\\\\\\\Y#RD1\\\Y}8\\\\\\\\\RE4\\\\\\\
    \\\\\RE4\\\\\\\\\\\\\\\\Y:\\Y1\\\Y>RD1\\\Yz}RD4RD2EGFC\”00\ \D3\1\5\5\5\BOGD\5\RB5
    GGGGGGFCZ5RB3\ 8:29\:29\3$\’\”00\5\$REE\RE1RD2z}RD4\RD3REEREE\RD3REE”\5\8\8\EGFC\”00\ \D3\1\5\5\5\BOGD\5\RB5
    GGGGGGFCZ5RB3\Z\$\ \4\$\\\\\$\\$YREF\.P\\\\

Leave a comment



Information for comment users
Line and paragraph breaks are implemented automatically. Your e-mail address is never displayed. Please consider what you're posting.

Use the buttons below to customise your comment.

RSS feed for comments on this post | TrackBack URI