Skip to content

Instantly share code, notes, and snippets.

@adsr

adsr/foo.php Secret

Created November 7, 2020 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adsr/2c0b9243986418af3cecf8046657304b to your computer and use it in GitHub Desktop.
Save adsr/2c0b9243986418af3cecf8046657304b to your computer and use it in GitHub Desktop.
<?php
// Proposal: Support `::function` syntax for sake of readability, static
// analysis, and consistency with `::class` syntax.
// For example, instead of:
call_user_func('printf', "hello\n");
// favor:
call_user_func(printf::function, "hello\n");
// Cases which some may find helpful/explicit, others may find confusing:
class X {
const F = printf::function;
static function sf($x) { return printf($x); }
function f($x) { return printf($x); }
static function ff() { return self::F; }
};
$x = new X();
call_user_func('printf'::function, "A\n");
call_user_func([$x, 'f']::function, "B\n");
call_user_func('X::sf'::function, "C\n");
call_user_func(X::F::function, "D\n");
call_user_func((fn ($x) => printf($x))::function, "E\n");
call_user_func((fn () => 'printf')()::function, "F\n");
// Needless case:
var_dump(printf::function::function::function);
// The proof-of-concept patch breaks in the case below. I think it may require
// altering ZEND_FETCH_CLASS_NAME, or emitting a new opcode, or reserving
// the field name `function` as we do with `class`.
class Y {
const function = 'Z';
}
var_dump(Y::function); // should be "Z", but is "Y"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment