Index

Path

MMO\core\helpers ยท 1 symbol ยท generated 30 Sep 2026

class

Path

Path.php:32
class MMO\core\helpers\Path implements MMO\core\interfaces\iArray, MMO\core\interfaces\iRenderable
Namespace
MMO\core\helpers
Implements
iArray, iRenderable
WARNING: separator in path MUST be '/' (linux) and not '\' (windows) # Array Path cannot be accessed with string keys. only int index works. If you access path with a negative number, it will returns the item at the index, starting from the end. ex: $path = new Path('/test/marc/lab'); $path == ['','test','marc','lab'] $path[-1] == 'lab' $path[-2] == 'marc' A path is split into a "basepath" (a fixed prefix, e.g. the project root) and a "relative path" (everything after it) -- [`getBaseIndex`](https://mmoframework.fr/documentation/mmo.core/php/helpers/Path.md#path-getbaseindex) / [`getBasepath`](https://mmoframework.fr/documentation/mmo.core/php/helpers/Path.md#path-getbasepath) / [`getRelpath`](https://mmoframework.fr/documentation/mmo.core/php/helpers/Path.md#path-getrelpath). The basepath boundary is set at construction (via the $root constructor argument) or later via [`rebase`](https://mmoframework.fr/documentation/mmo.core/php/helpers/Path.md#path-rebase) / [`setBaseIndex`](https://mmoframework.fr/documentation/mmo.core/php/helpers/Path.md#path-setbaseindex) / [`setBasepath`](https://mmoframework.fr/documentation/mmo.core/php/helpers/Path.md#path-setbasepath).
$p = new Path('/home/user/project/src/file.php', 2); // basepath ends at index 2 $p->getBasepath(); // '/home/' $p->getRelpath(); // 'user/project/src/file.php' $p->getExtension(); // 'php'

Index

ABS_FILENAMEget absolute filename
BASENAMEget only the file's name and extension
BASE_PATHinclude url base path
EXTENSIONinclude url extension
FILEinclude url file
PATHget only the file's path
REL_FILENAMEget only the relative filename
REL_PATHinclude url relative path
STATUS_WINDOWS
base
content
immutable
windows
addPath($path)Append $path (via merge, which resolves any '..'/'.' against the existing content).
clear()Empty this path entirely.
count()\Countable::count
debugName()
empty()
flags()Get set parts of the filename
FlagsText($flags, $sep) staticConvert binary url flags to list of strings
getBaseIndex()
getBasename()Get the filename on the path
getBasepath($join)Get the basepath. If non empty, it is always returned with en ending '/'
getExtension()
getFile()
getIterator()\IteratorAggregate::getIterator
getRelpath($join)Get the relative part of the path
getSimplified($join)
hasBasename()Tells if path has a filename at the end of its path
hasBasepath()
hasRelpath($strict)
Immutable($path, $root, $isWindows) staticSame as the constructor, but the returned Path is isImmutable.
isAbsolute()
isImmutable()
isWindows()
jsonSerialize()
offsetExists($key)\ArrayAccess::offsetExists
offsetGet($key)\ArrayAccess::offsetGet -- a negative $key counts from the end (-1 is the last segment).
offsetSet($key, $value)\ArrayAccess::offsetSet -- a negative $key counts from the end, like offsetGet; null appends.
offsetUnset($key)\ArrayAccess::offsetUnset -- a negative $key counts from the end, like offsetGet.
rebase($root)Make path absolute to a root.
relativeTo($root, $strict)Make path relative to a root
removePath($number)Remove the last $number segments.
render($opts)Render the path
set($path)Set the whole path and reset the basepath
setBaseIndex($index)Set the base index (used internally)
setBasename($file)Replace the last segment (the filename, if any) with $file.
setBasepath($path)Set the basepath, keeping the existing relative path.
setExtension($ext)Replace the filename's extension, keeping its name.
setFile($file)Replace the filename's name, keeping its extension.
setRelpath($path)Set the relative part of the path, keeping the existing basepath.
setWindows($value)
simplify()Simplifies path, removing `, .. and .` WARNING: change the basepath index if the simplified path reduced it
slice($offset, $length, $join)
toArray()
toImmutable($forceClone)
toMutable($forceClone)
__construct($path, $root, $isWindows)constructor
__toString()

Constants9

ABS_FILENAME

get absolute filename

const ABS_FILENAME = 480

BASENAME

get only the file's name and extension

const BASENAME = 384

BASE_PATH

include url base path

const BASE_PATH = 32

EXTENSION

include url extension

const EXTENSION = 256

FILE

include url file

const FILE = 128

PATH

get only the file's path

const PATH = 96

REL_FILENAME

get only the relative filename

const REL_FILENAME = 448

REL_PATH

include url relative path

const REL_PATH = 64

STATUS_WINDOWS

const STATUS_WINDOWS = 2

Properties4

base

protected int $base
Type
int

content

protected array $content
Type
array

immutable

protected bool $immutable = false
Type
bool

windows

protected bool $windows = false
Type
bool

Methods46

addPath($path)

Path.php:350

Append $path (via merge, which resolves any '..'/'.' against the existing content).

function addPath($path): void
$path
returnsvoid
throwsif isImmutable
(new Path('/a/b/c'))->addPath('d/e'); // '/a/b/c/d/e'

debugName()

Path.php:168
function debugName(): string
returnsstringe.g. '(path)/test/marc/lab'

empty()

Path.php:148
function empty(): bool
returnsbooltrue if this path has no segments at all

flags()

Path.php:197

Get set parts of the filename

function flags(): int
returnsint$flags binary combination of self::* constant

FlagsText($flags, $sep)

staticPath.php:179

Convert binary url flags to list of strings

static function FlagsText($flags, $sep = ',')
$flagsthe flags to test
$sep = ','the separator to use to concat the values, or false to return as array
returns
Path::FlagsText(Path::BASE_PATH | Path::FILE); // 'basepath,file'

getBaseIndex()

Path.php:227
function getBaseIndex(): int
returnsintthe segment index where the basepath ends and the relative path begins

getBasename()

Path.php:285

Get the filename on the path

function getBasename(): ?string
returnsnull | string
(new Path('/a/b/.gitignore'))->getBasename(); // '.gitignore'

getBasepath($join)

Path.php:213

Get the basepath. If non empty, it is always returned with en ending '/'

function getBasepath(bool $join = true): array|string
$join = truebool
returnsarray | string

getExtension()

Path.php:295
function getExtension(): ?string
returnsnull | stringthe extension (without the '.'), or null if the last segment has no '.'

getFile()

Path.php:290
function getFile(): ?string
returnsnull | stringthe filename without its extension, or null if the last segment has no '.'

getRelpath($join)

Path.php:249

Get the relative part of the path

function getRelpath(bool $join = true): array|string
$join = truebool
returnsarray | string

getSimplified($join)

Path.php:466
function getSimplified(bool $join = true): array|string
$join = truebool
returnsarray | stringthe simplified path (resolving '.'/'..'), without mutating this instance -- see simplify for that

hasBasename()

Path.php:274

Tells if path has a filename at the end of its path

function hasBasename(): bool
returnsbool

hasRelpath($strict)

Path.php:264
function hasRelpath(bool $strict = false): bool
$strict = falsebool
returnsbool
(new Path('/a/b/file.txt', 1))->hasRelpath(); // true(new Path('/a', 2))->hasRelpath(); // false: the basepath already consumes the whole path

Immutable($path, $root, $isWindows)

staticPath.php:79

Same as the constructor, but the returned Path is isImmutable.

static function Immutable( array|string $path = '', string|int|null $root = null, bool $isWindows = MMO\core\helpers\IS_WINDOWS )
$path = ''array | string
$root = nullstring | int | null
$isWindows = MMO\core\helpers\IS_WINDOWSbool
returns

isAbsolute()

Path.php:143
function isAbsolute(): bool
returnsbooltrue if this path starts from the filesystem/URL root

isImmutable()

Path.php:111
function isImmutable(): bool
returnsbooltrue if this Path rejects mutation (every setter throws eRuntime)

offsetExists($key)

Path.php:588

\ArrayAccess::offsetExists

function offsetExists(mixed $key): bool
$keymixed
returnsbool

offsetGet($key)

Path.php:557

\ArrayAccess::offsetGet -- a negative $key counts from the end (-1 is the last segment).

function offsetGet(mixed $key): mixed
$keymixed
returnsmixed
$path[-1]; // the last segment (e.g. the filename)$path[0]; // '' for an absolute path (the leading empty segment before the first '/')

offsetSet($key, $value)

Path.php:565

\ArrayAccess::offsetSet -- a negative $key counts from the end, like offsetGet; null appends.

function offsetSet(mixed $key, mixed $value): void
$keymixed
$valuemixed
returnsvoid

offsetUnset($key)

Path.php:545

\ArrayAccess::offsetUnset -- a negative $key counts from the end, like offsetGet.

function offsetUnset(mixed $key): void
$keymixed
returnsvoid
unset($path[-1]); // removes the last segment

relativeTo($root, $strict)

Path.php:502

Make path relative to a root

function relativeTo(self|array|string $root, bool $strict = false): bool
$rootself | array | stringthe base path
$strict = falseboolif true, path MUST be a valid descendant of root (.. forbidden). returns false if not.
returnsboolfalse if could not make path relative to root
throwsif isImmutable
(new Path('/a/b/c/d'))->relativeTo('/a/b'); // true; the path becomes 'c/d'

removePath($number)

Path.php:367

Remove the last $number segments.

function removePath(int $number = 1): void
$number = 1int
returnsvoid
throwsif isImmutable
(new Path('/a/b/c/d'))->removePath(2); // '/a/b'

render($opts)

Path.php:124

Render the path

function render(array $opts = []): string
$opts = []arraybasepath: if false, remove the basepath if string, render the relative path compare to basepath
returnsstring
(new Path('/home/user/file.php', 1))->render(); // '/home/user/file.php'(new Path('/home/user/file.php', 1))->render(['basepath' => false]); // 'home/user/file.php'

setBaseIndex($index)

Path.php:382

Set the base index (used internally)

function setBaseIndex(int $index): void
$indexintmust be within [0, count(this path's segments)]
returnsvoid
throwsif isImmutable
throwsif $index is out of range

setBasename($file)

Path.php:428

Replace the last segment (the filename, if any) with $file.

function setBasename(string $file): void
$filestring
returnsvoid
throwsif isImmutable
(new Path('/a/b/old.txt'))->setBasename('new.md'); // '/a/b/new.md'

setExtension($ext)

Path.php:456

Replace the filename's extension, keeping its name.

function setExtension(string $ext): void
$extstringwithout the leading '.'
returnsvoid
throwsif isImmutable, or if there is no existing filename to keep
(new Path('/a/b/file.txt'))->setExtension('md'); // '/a/b/file.md'

setFile($file)

Path.php:442

Replace the filename's name, keeping its extension.

function setFile(string $file): void
$filestring
returnsvoid
throwsif isImmutable, or if there is no existing extension to keep
(new Path('/a/b/old.txt'))->setFile('new'); // '/a/b/new.txt'

setWindows($value)

Path.php:158
function setWindows(bool $value): void
$valueboolwhether to use Windows path conventions ('\\' separator, drive letters, ...)
returnsvoid

simplify()

Path.php:477

Simplifies path, removing `, .. and .` WARNING: change the basepath index if the simplified path reduced it

function simplify(): self
returnsself$this, for chaining
throwsif isImmutable
(new Path('/a/./b/../c'))->simplify(); // '/a/c'

slice($offset, $length, $join)

Path.php:238
function slice(int $offset, ?int $length = null, bool $join = false): array|string
$offsetint
$length = nullnull | int
$join = falsebool
returnsarray | stringsame segment-slicing as array_slice(), collapsing the special '/' (root-only) case to an empty result

toArray()

Path.php:138
function toArray(): array
returnsarraythe path split into segments, e.g. ['', 'test', 'marc', 'lab'] for '/test/marc/lab'

toImmutable($forceClone)

Path.php:90
function toImmutable(bool $forceClone = false): self
$forceClone = falseboolif false (the default) and this Path is already immutable, return $this as-is instead of cloning
returnsselfan immutable Path with the same content

toMutable($forceClone)

Path.php:101
function toMutable(bool $forceClone = false): self
$forceClone = falseboolif true, always clone even if this Path is already mutable
returnsselfa mutable Path with the same content -- $this itself if it was already mutable and $forceClone is false

__construct($path, $root, $isWindows)

Path.php:67

constructor

function __construct( array|string $path = '', string|int|null $root = null, bool $isWindows = MMO\core\helpers\IS_WINDOWS )
$path = ''array | string
$root = nullstring | int | nullthe root index or the root path of this path
$isWindows = MMO\core\helpers\IS_WINDOWSbool