On Sat, 2024-09-14 at 15:33 +0000, Gina P. Banyard wrote:
> Hello internals,
> 
> I came across the Directory class while doing some code exploration
> of ext/standard.
> This class is effectively an opaque object for Directory resources,
> however it doesn't behave like one, as it has existed since PHP 4.
> 
> As such, I am proposing an RFC to turn this class into an opaque
> object.
> 
> RFC: https://wiki.php.net/rfc/directory-opaque-object
> 
> 
> Best regards,
> 
> Gina P. Banyard


I don't suppose we could call the Directory class something else, like
"SPLDirectory"?

You can't make a Directory with `new` (or at least you aren't supposed
to be able to), so any existing code would already get a Directory
object with `dir()` not `new Directory()`. There would be no BC break
with a rename.

Why?

It's very easy for someone that doesn't know about the Directory class
to create their own class called `Directory`, instantiate it with `new`
expecting their autoloader to pick it up, and then getting very strange
results.

The error will usually be something like, "call to undefined method
Directory::MyMethod", which makes developers go nuts, looking at their
Directory class, seeing "MyMethod" clearly defined right there, and yet
PHP claims it can't find the method. But PHP is finding the **class**
because surely, the autoloader loaded it!

What actually happened is the autoloader didn't run because the name
"Directory" is already registered. But there is no warning or error
when someone writes `new Directory();`.

Since the built-in Directory class is never instantiated by name,
nothing would break by renaming it, since it only gets created by
`dir()`.

A rename would free up the name 'Directory' so people wouldn't get
frustrated with bizarre behavior when they make their own Directory
class (which I imagine is very common).

Reply via email to