PHP でデフォルトの関数呼び出しを属性にできないのはなぜですか?

Susan Sarandon
リリース: 2024-10-17 20:34:02
オリジナル
293 人が閲覧しました

Why Can\'t Attribute Defaults Function Calls in PHP?

Can't Call Functions in PHP Attribute Defaults

[Problem]

Despite having previous programming experience, a novice in PHP is perplexed by an attribute default error. The code:

<code class="php">class Foo {
    public $path = array(
        realpath(".")
    );
}</code>
ログイン後にコピー

yields a syntax error. However, the following works seamlessly:

<code class="php">$path = array(
    realpath(".")
);</code>
ログイン後にコピー

The question arises: why can't functions be invoked in attribute defaults? Is this intentional or a flaw in implementation?

[Answer]

The PHP compiler code indicates this restriction is intentional, although no official rationale is available. Implementing this functionality reliably poses certain challenges, as evidenced by limitations in PHP's current implementation.

The compiler's grammar defines a class variable declaration as:

class_variable_declaration:
      //...
      | T_VARIABLE '=' static_scalar //...
;
ログイン後にコピー

Therefore, to assign variable values like $path, the expected value must align with a static scalar. This encompasses arrays with values that are also static scalars:

static_scalar: /* compile-time evaluated scalars */
      //...
      | T_ARRAY '(' static_array_pair_list ')' // ...
      //...
;
ログイン後にコピー

If the grammar allowed the following syntax, which aligns with the code sample, the script would encounter a "Invalid binding type" error:

class_variable_declaration:
      //...
      | T_VARIABLE '=' T_ARRAY '(' array_pair_list ')' // ...
;
ログイン後にコピー

Parsing the given code sample reveals the following steps:

zend_do_begin_class_declaration() // Adds an opcode
array_init(), zend_do_add_static_array_element() // Do not create new opcodes, add array to class properties
zend_do_declare_property() // Declares the property
zend_do_early_binding() // Consumes the last opcode and evaluates it
ログイン後にコピー

If the opcode is not expected (e.g., related to functions or methods), an error is thrown.

Allowing non-static arrays generates an INIT_ARRAY opcode, which disrupts zend_do_early_binding():

DECLARE_CLASS   'Foo'
SEND_VAL        '.'
DO_FCALL        'realpath'
INIT_ARRAY
ログイン後にコピー

To accommodate function calls in attribute defaults, a new opcode array scoped to the class variable declaration would be needed, similar to method definitions. However, determining the timing of such evaluation presents additional challenges.

Other dynamic languages have managed to resolve this, but it remains a feature absent in PHP, potentially due to its complexity and perceived low priority.

以上がPHP でデフォルトの関数呼び出しを属性にできないのはなぜですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
著者別の最新記事
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!