# RFE to allow dirname($foo, 2)

- Thread: https://externals.io/message/87227
- Original archive: https://news-web.php.net/php.internals/87227
- Started by Remi Collet on 2015-07-22T11:55:45+00:00
- Messages: 14
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #87227 — Remi Collet — 2015-07-22T11:55:45+00:00

URL: https://externals.io/message/87227#87227

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

See https://bugs.php.net/bug.php?id=70112


As this a very small self-contained change,
I don't think it needs a RFE.

Feedback welcome


Remi.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iEYEARECAAYFAlWvhMEACgkQYUppBSnxahh+HACg18VjI/eZpBXAfXrHD+KOjCpb
IAgAoN33DaqH0j4kTe1FTceeL3T3OweQ
=yaJf
-----END PGP SIGNATURE

---

## #87228 — flaupretre@free.fr — 2015-07-22T12:09:41+00:00

URL: https://externals.io/message/87227#87228 · In reply to: #87227

Hi,

> De: "Remi Collet" <remi@fedoraproject.org>
>
> See https://bugs.php.net/bug.php?id=70112
>
> As this a very small self-contained change, I don't think it needs a RFE.

+1 for the feature, but not sure it can go to 7.0.

Regards

François

---

## #87235 — Sara Golemon — 2015-07-22T18:49:27+00:00

URL: https://externals.io/message/87227#87235 · In reply to: #87227

> See https://bugs.php.net/bug.php?id=70112
>
I'm not inherently against it, but this really really sounds like a
job for a userspace function.

function dirname_n($path, $n) {
  while (($path !== '.') && $n--) $path = dirname($path);
  return $path;
}

-Sara

---

## #87236 — Marco Pivetta — 2015-07-22T18:57:23+00:00

URL: https://externals.io/message/87227#87236 · In reply to: #87235

> On Wed, Jul 22, 2015 at 4:55 AM, Remi Collet <remi@fedoraproject.org>
> wrote:
> > […]
> I'm not inherently against it, but this really really sounds like a
> job for a userspace function.
>
> function dirname_n($path, $n) {
>   while (($path !== '.') && $n--) $path = dirname($path);
>   return $path;
> } <http://www.php.net/unsub.php>
>
>
Indeed.
What need is there to add a functionality to a relatively simple function
like this one?
What's the actual advantage that cannot be achieved in userland code?

Greets,

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/

---

## #87242 — Remi Collet — 2015-07-23T05:28:56+00:00

URL: https://externals.io/message/87227#87242 · In reply to: #87236

Le 22/07/2015 20:57, Marco Pivetta a écrit :
> On 22 July 2015 at 19:49, Sara Golemon <pollita@php.net 
> <mailto:pollita@php.net>> wrote:
> 
> On Wed, Jul 22, 2015 at 4:55 AM, Remi Collet
> <remi@fedoraproject.org <mailto:remi@fedoraproject.org>> wrote:
> > […]
> I'm not inherently against it, but this really really sounds like
> a job for a userspace function.
> 
> function dirname_n($path, $n) { while (($path !== '.') && $n--)
> $path = dirname($path); return $path; 
> }<http://www.php.net/unsub.php>
> 
> 
> Indeed. What need is there to add a functionality to a relatively
> simple function like this one? What's the actual advantage that
> cannot be achieved in userland code?
> 

Legibility and the frequency of the usage of dirname(dirname(...
in various pieces of code.

See
https://github.com/search?q=dirname+__DIR__+language%3Aphp&ref=searchresults&type=Code&utf8=%E2%9C%93

As github search doesn't allow to search for (, I use __dir__ which
probably hides more usage.

As "n" is usually 2 or 3, nobody will write a userland function.


Remi

> Greets,
> 
> Marco Pivetta
> 
> http://twitter.com/Ocramius
> 
> http://ocramius.github.com/

---

## #87243 — Josh Di Fabio — 2015-07-23T09:54:44+00:00

URL: https://externals.io/message/87227#87243 · In reply to: #87235

Great suggestion, Remi, I'd love to see this change!


>
> 
> > […]
> I'm not inherently against it, but this really really sounds like a
> job for a userspace function.
>
> function dirname_n($path, $n) {
>   while (($path !== '.') && $n--) $path = dirname($path);
>   return $path;
> }
>
> -Sara

Let's assume that we implement this function in userland, either in a Composer
package or more likely in a functions.php file in our project, which Composer or
our project's bootstrap includes for us.

Now, most often, dirname(... dirname(__DIR__) ...) is used in application entry
points during the bootstrapping process. In my experience, it's most commonly
used in order to include an autoloader or some bootstrap file which itself is
responsible for including the autoloader.

In other words, dirname(...) is generally used before we have access to our
includes and classes (no autoloader). Which means, if we were to simply
implement this in userland, we'd have to either write require
dirname(...) in order
to load the dirname_n function every time we need to use it, which would
obviously defeat the object, or conditionally declare the function everywhere
it's needed, which wouldn't be ideal.

Is there some more elegant way this could work in userland which I've missed?

---

## #87244 — Anatol Belski — 2015-07-23T10:17:47+00:00

URL: https://externals.io/message/87227#87244 · In reply to: #87242

Hi,

> -----Original Message-----
> From: Remi Collet [mailto:remi@fedoraproject.org]
> Sent: Thursday, July 23, 2015 7:29 AM
> To: PHP Internals <internals@lists.php.net>
> Subject: Re: [PHP-DEV] RFE to allow dirname($foo, 2)
> 
> Le 22/07/2015 20:57, Marco Pivetta a écrit :
> > […]
> 
> Legibility and the frequency of the usage of dirname(dirname(...
> in various pieces of code.
> 
> See
> https://github.com/search?q=dirname+__DIR__+language%3Aphp&ref=searchr
> esults&type=Code&utf8=%E2%9C%93
> 
> As github search doesn't allow to search for (, I use __dir__ which probably hides
> more usage.
> 
> As "n" is usually 2 or 3, nobody will write a userland function.
> 
IMHO the idea is not doing any harm while solving a real use case. I wouldn't resist to take it in, especially if there are more useful cases and we can test the patch early.

One more note is that it's somehow logic coupled with mkdir having the recursive option. Also it's useful for including the autoloader as no custom library functions are available yet. A downside could be - it will encourage people writing a non PHP5 compatible code (or maybe it's not an issue).

Regards

Anatol

---

## #87257 — Hannes Magnusson — 2015-07-24T00:11:22+00:00

URL: https://externals.io/message/87227#87257 · In reply to: #87227

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> See https://bugs.php.net/bug.php?id=70112
>
>
> As this a very small self-contained change,
> I don't think it needs a RFE.
>
> Feedback welcome




Shouldn't you check if you are at the root (or cwd)?
currently dirname("/foo/bar", PHP_INT_MAX); "hangs"


-Hannes

---

## #87258 — Scott Arciszewski — 2015-07-24T00:19:53+00:00

URL: https://externals.io/message/87227#87258 · In reply to: #87228

> Hi,
>
> > […]
>
> +1 for the feature, but not sure it can go to 7.0.
>
> Regards
>
> François
>
> --
> 
> 
>

+1 from me

+1 from me on adding it to PHP 7.0.0 as well. And not just to be
contrary, I'd like it to be another selling point for upgrading
swiftly. :)

Though my second vote probably won't count for anything.

Scott Arciszewski
Chief Development Officer
Paragon Initiative Enterprises <https://paragonie.com

---

## #87265 — Remi Collet — 2015-07-24T08:39:47+00:00

URL: https://externals.io/message/87227#87265 · In reply to: #87257

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Le 24/07/2015 02:11, Hannes Magnusson a écrit :
> On Wed, Jul 22, 2015 at 4:55 AM, Remi Collet
> <remi@fedoraproject.org> wrote:
> > […]
> 
> 
> 
> 
> Shouldn't you check if you are at the root (or cwd)? currently
> dirname("/foo/bar", PHP_INT_MAX); "hangs"

Good catch.
Should be ok now.

Remi.
> 
> 
> -Hannes
> 

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iEYEARECAAYFAlWx+dMACgkQYUppBSnxahjuPACgxZ+yLXbLzQXZKmJhWAbH0OD9
jqAAnjImkjL6j+p4dsRC0IC8N6fRKFXc
=4YM4
-----END PGP SIGNATURE

---

## #87288 — Dennis Birkholz — 2015-07-25T14:11:38+00:00

URL: https://externals.io/message/87227#87288 · In reply to: #87243

Hi,

Am 23.07.2015 um 11:54 schrieb Josh Di Fabio:
> Now, most often, dirname(... dirname(__DIR__) ...) is used in application entry
> points during the bootstrapping process. In my experience, it's most commonly
> used in order to include an autoloader or some bootstrap file which itself is
> responsible for including the autoloader.

doesn't work __DIR__.'/../../' on Windows also? Why do you need
 `dirname()` here?

Greets
Dennis

---

## #87292 — Scott Arciszewski — 2015-07-25T17:42:11+00:00

URL: https://externals.io/message/87227#87292 · In reply to: #87288

> Hi,
>
> Am 23.07.2015 um 11:54 schrieb Josh Di Fabio:
> > […]
>
> doesn't work __DIR__.'/../../' on Windows also? Why do you need
> `dirname()` here?
>
> Greets
> Dennis
>
> --
> 
> 
>

What's easier to read and less likely to result in bugs?

require_once __DIR__ . '/../../../../autoload.php';

or

require_once dirname(__FILE__, 5) . '/autoload.php';

Scott Arciszewski
Chief Development Officer
Paragon Initiative Enterprises <https://paragonie.com

---

## #87293 — Dennis Birkholz — 2015-07-25T18:11:48+00:00

URL: https://externals.io/message/87227#87293 · In reply to: #87292

Hi,

Am 25.07.2015 um 19:42 schrieb Scott Arciszewski:
> 
> > […]
> 
> What's easier to read and less likely to result in bugs?
> 
> require_once __DIR__ . '/../../../../autoload.php';
> or
> require_once dirname(__FILE__, 5) . '/autoload.php';

I am not going to discuss personal preferences, but for me the first is.
You require to go four Levels up and include something, that should just
not be necessary with proper autoloading.

Greets,
Dennis

---

## #87301 — kelerest123@gmail.com — 2015-07-26T18:44:15+00:00

URL: https://externals.io/message/87227#87301 · In reply to: #87292

Hi Scott!

2015-07-25 19:42 GMT+02:00 Scott Arciszewski <scott@paragonie.com>:
> What's easier to read and less likely to result in bugs?
>
> require_once __DIR__ . '/../../../../autoload.php';
>
> or
>
> require_once dirname(__FILE__, 5) . '/autoload.php';
That's on everyone's own, but for me it's the first. However, it
couldn't be a disaster having ability to choose other option for
those, who are not familiar with relative paths. :-)


Best regards,
Kubo2, Jakub Kubíček
