Skip to content

use method aliasing/importing #18623

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
staabm opened this issue May 22, 2025 · 3 comments
Open

use method aliasing/importing #18623

staabm opened this issue May 22, 2025 · 3 comments

Comments

@staabm
Copy link
Contributor

staabm commented May 22, 2025

Description

use of static methods can get pretty bulky and leads to pretty long expressions, see

class HtmlHelper {
    public static function escape_htmlspecial(string $string): string
    {
       return htmlspecialchars($string, \ENT_QUOTES, ENCODING_CHARSET, false);
    }
}
?>
<div class="page-heading">
    <h2><?php echo HtmlHelper::escape_htmlspecial($someString); ?></h2>
</div>

(html and php code would obvisouly be separated in 2 files.. putting them together for demonstration purposes)

static method have the benefit of beeing autoloaded though.


I could use namespaced functions instead:

namespace HtmlHelper {
    function escape_htmlspecial(string $string): string
    {
       return htmlspecialchars($string, \ENT_QUOTES, ENCODING_CHARSET, false);
    }
}

use function HtmlHelper\escape_htmlspecial as html_esc;
?>
<div class="page-heading">
    <h2><?php echo html_esc($someString); ?></h2>
</div>

(html and php code would obvisouly be separated in 2 files.. putting them together for demonstration purposes)

that way I can use function and optionally import aliases to shrink it even further.
this has the problem that I no longer can rely on native autoloading though.
its easier to read


I am thinking whether it would be possible to combine the benefits of the 2 approaches

Proposal:

class HtmlHelper {
    public static function escape_htmlspecial(string $string): string
    {
       return htmlspecialchars($string, \ENT_QUOTES, ENCODING_CHARSET, false);
    }
}

use function HtmlHelper::escape_htmlspecial as html_esc;

?>
<div class="page-heading">
    <h2><?php echo html_esc($someString); ?></h2>
</div>

-> new use function syntax (to be discussed)
-> for static methods only
-> autoloading of the HtmlHelper class
-> readability of the function imported aliases (import alias only optional)

wdyt?

@nielsdos
Copy link
Member

This reminds me of import static from Java. I guess it could make sense for PHP too.
This is likely not hard to implement, but there are a few questions around edge cases that need to be solved:

  • How to handle name clashes with a local function of the same name as an alias
  • Should the alias be usable as a string-based callable?

Anyway, since this is a language change this requires an RFC.

@jorgsowa
Copy link
Contributor

Wouldn't the function autoloading resolve this problem?

https://wiki.php.net/rfc/core-autoloading

@mvorisek
Copy link
Contributor

mvorisek commented May 27, 2025

If implemented like use function HtmlHelper::escape_htmlspecial as html_esc;, ie. use function with :: support, then the answers for the questions are:

* How to handle name clashes with a local function of the same name as an alias

as already supported - https://3v4l.org/0alOH

* Should the alias be usable as a string-based callable?

yes - https://3v4l.org/4lAUT

The possible downside is a small ambiguity when aliased import should be preferred over regular class import. IMO, within regular classes, never.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants