这是什么样的PHP语法?

问题描述 投票:0回答:1

你能告诉我 PHP 语法是什么样的吗:

echo Html::{'div.my-class#my-id'}('This is element content')

来自 https://github.com/decodelabs/tagged 库。

我的意思是我知道它的作用,但我不明白所使用的语法及其工作原理:)(紧接在作用域运算符之后的大括号)。看起来他们正在使用大括号来“生成”要调用的函数的名称,但另一方面,它包含诸如“.”之类的字符。和“#”以及变量参数(“my-class”、“my-id”)。我很困惑......

我尝试用谷歌搜索两个多小时,但没有成功:/

php syntax
1个回答
0
投票

以下代码没有回答问题,但显示了一个工作示例:

class weird_php_magic
{
    public static function __callStatic(
        string $method,
        array $args
    ): string|Stringable|null {
        return static::convert($method, ...$args);
    }
    
    public static function convert(
        string $name,
        mixed $content,
        ?array $options = [],
        ?callable $setup = null
    ): string|Stringable|null {
        var_dump($name);
        var_dump($content);
        
        return null;
    }
}

weird_php_magic::{'this does not exist'}('nor this');

输出

string(19) "this does not exist"
string(8) "nor this"

我用 PHP 8.2 测试了这段代码。

© www.soinside.com 2019 - 2024. All rights reserved.