php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80914 function_exists is now case-sensitive for aliases
Submitted: 2021-03-29 14:45 UTC Modified: 2021-04-14 08:54 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: chris at vendiadvertising dot com Assigned: nikic (profile)
Status: Closed Package: *General Issues
PHP Version: 8.0.3 OS: All
Private report: No CVE-ID: None
 [2021-03-29 14:45 UTC] chris at vendiadvertising dot com
Description:
------------
Prior to PHP 8, a function could use the same name as a built-in function's alias as long as it changed the case. For example, the built-in function dir has an internal alias of getdir, and user-land code could create a function called getDir without issue. As of PHP 8, doing so will raise a Fatal error: Cannot redeclare xyz.

https://3v4l.org/hdHSS

This was originally noted on SO https://stackoverflow.com/q/66854655/231316

It appears that Bug #79382 (Cannot redeclare disabled function) was fixed in https://github.com/php/php-src/commit/53eee290b6f5ca531aef19885a392c939013ce36. That fix uses a simple lower-case lookup of the provided function's name.

To be clear, overriding a built-in function, alias or not, is probably not a good idea, and I don't know if this should be fixed. But it is an unexpected breaking change for legacy code and it should at least be documented.

Test script:
---------------
var_dump(function_exists('getDir'));

Expected result:
----------------
In PHP prior to 8, the check returned false.

Actual result:
--------------
Starting with PHP 8 this returns true

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-03-29 15:13 UTC] egegeg dot egeez at fff dot ff
functions in php are generally case-insensitive and every place where they are not is and was a bug by definition - so this is a bugfix and not a bug

writing plain functions without a prefix is bad coding style anyways because you never know if a future version brings a native function with the same name but different semantics 

calling a function identical as a builtin but with different case is asking for troubles no matter what
 [2021-03-29 15:20 UTC] chris at vendiadvertising dot com
I absolutely agree that writing a function with the same name as a built-in but with different case is asking for trouble. But is there a list of aliases somewhere to help someone troubleshoot this? Looking at getdir specifically, searching the manual doesn't provide anything, and it is not listed as an alias for dir. Maybe this is just a documentation bug then?
 [2021-03-29 17:54 UTC] chris at vendiadvertising dot com
I went through all 25 aliases listed in https://github.com/php/php-src/blob/master/ext/standard/basic_functions.stub.php and it seems that getdir is the only only that is not listed in the PHP docs, so I'm fine reclassifying this as a documentation update for that specific function.

I do still think, however, with how many aliases exist in PHP that this could still be a problem for future upgraders.
 [2021-04-06 20:10 UTC] rowan dot collins at gmail dot com
There's actually something a lot weirder going on here.

Firstly, looking closely at the source, it seems that internally, getdir() is the *real* name of the function, and dir() is the alias.

Secondly, there is no case-sensitivity change, and function_exists() was telling the truth, getdir() really *didn't* exist on older versions of PHP: https://3v4l.org/dCpeh

It seems that this function had been there for years as some kind of ghost, and a change in PHP 8 brought it to life.
 [2021-04-11 19:29 UTC] rowan dot collins at gmail dot com
The problem here is actually not related to case-sensitivity. 

The function/alias getdir() didn't exist until PHP 8.0, and it turns out it was actually added by mistake.

For the full story, see https://externals.io/message/113982 and https://stackoverflow.com/a/66981991/157957

The current plan is to remove it in the next 8.0.x release.
 [2021-04-11 19:30 UTC] rowan dot collins at gmail dot com
The following pull request has been associated:

Patch Name: Remove the "getdir" function which was introduced accidentally
On GitHub:  https://github.com/php/php-src/pull/6855
Patch:      https://github.com/php/php-src/pull/6855.patch
 [2021-04-14 08:54 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2021-04-14 08:54 UTC] nikic@php.net
Closing as https://github.com/php/php-src/pull/6855 has been merged.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 14:01:29 2024 UTC