php / php/php-src

Deprecation errors are inconsistently triggered for underscore class aliases

Open
#18,285 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Status: Needs Triage
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Description

The following code:

<?php

namespace Foo {
    class __ {}
}

namespace Bar {
    class _ {} // Deprecated
}

namespace {
    class __ {}
    
    class_alias('Foo\__', 'Foo\_'); // Is this meant to be deprecated?
    class_alias('__', '_'); // Deprecated
}

namespace Baz {
    use Foo\_;
}

namespace Qux {
    use Foo\__ as _; // Is this meant to be deprecated?
}

Resulted in this output:

Deprecated: Using "_" as a class name is deprecated since 8.4 in /in/ZZMJs on line 8

Deprecated: Using "_" as a class alias is deprecated since 8.4 in /in/ZZMJs on line 15

But I expected this output instead:

Deprecated: Using "_" as a class name is deprecated since 8.4 in /in/ZZMJs on line 8

Deprecated: Using "_" as a class alias is deprecated since 8.4 in /in/ZZMJs on line 14

Deprecated: Using "_" as a class alias is deprecated since 8.4 in /in/ZZMJs on line 15

Deprecated: Using "_" as a class alias is deprecated since 8.4 in /in/ZZMJs on line 23

I encountered this issue while trying to migrate a large closed source project to PHP 8.4. The namespaced _ class was used as a wrapper for the gettext _ function (eg new _("string")), to delay performing the translation of cached content until the http response is stringified. The class had ~35k instantiations in source control and more instances in compressed + serialized database records. This made class aliases a potentially appealing solution, but I understand from @Girgias that these not emitting deprecation errors is an oversight.

PHP Version

8.4.1 - 8.4.6 on 3v4l

Operating System

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied PHP 8.4 reproduction and compare deprecation behavior for class declarations, class_alias() calls, and the namespaced use alias. Trace the interpreter paths handling class aliases and namespace imports, then add regression coverage showing that all four expected underscore cases emit deprecations.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.