Skip to content
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

php8.4-属性钩子 #848

Open
guanhui07 opened this issue Nov 24, 2024 · 0 comments
Open

php8.4-属性钩子 #848

guanhui07 opened this issue Nov 24, 2024 · 0 comments
Labels

Comments

@guanhui07
Copy link
Owner

guanhui07 commented Nov 24, 2024

属性钩子

属性钩子提供对计算属性的支持,这些属性可以被 IDE 和静态分析工具直接理解,而无需编写可能会失效的 docblock 注释。此外,它们允许可靠地预处理或后处理值,而无需检查类中是否存在匹配的 getter 或 setter。

PHP < 8.4

class Locale
{
    private string $languageCode;
    private string $countryCode;

    public function __construct(string $languageCode, string $countryCode)
    {
        $this->setLanguageCode($languageCode);
        $this->setCountryCode($countryCode);
    }

    public function getLanguageCode(): string
    {
        return $this->languageCode;
    }

    public function setLanguageCode(string $languageCode): void
    {
        $this->languageCode = $languageCode;
    }

    public function getCountryCode(): string
    {
        return $this->countryCode;
    }

    public function setCountryCode(string $countryCode): void
    {
        $this->countryCode = strtoupper($countryCode);
    }

    public function setCombinedCode(string $combinedCode): void
    {
        [$languageCode, $countryCode] = explode('_', $combinedCode, 2);

        $this->setLanguageCode($languageCode);
        $this->setCountryCode($countryCode);
    }

    public function getCombinedCode(): string
    {
        return \sprintf("%s_%s", $this->languageCode, $this->countryCode);
    }
}

$brazilianPortuguese = new Locale('pt', 'br');
var_dump($brazilianPortuguese->getCountryCode()); // BR
var_dump($brazilianPortuguese->getCombinedCode()); // pt_BR

php8.4

class Locale
{
    public string $languageCode;

    public string $countryCode
    {
        set (string $countryCode) {
            $this->countryCode = strtoupper($countryCode);
        }
    }

    public string $combinedCode
    {
        get => \sprintf("%s_%s", $this->languageCode, $this->countryCode);
        set (string $value) {
            [$this->countryCode, $this->languageCode] = explode('_', $value, 2);
        }
    }

    public function __construct(string $languageCode, string $countryCode)
    {
        $this->languageCode = $languageCode;
        $this->countryCode = $countryCode;
    }
}

$brazilianPortuguese = new Locale('pt', 'br');
var_dump($brazilianPortuguese->countryCode); // BR
var_dump($brazilianPortuguese->combinedCode); // pt_BR
class BookViewModel
{
    public function __construct(
        private array $authors,
    ) {}

    public string $credits {
        get {
            return implode(', ', array_map(
                fn (Author $author) => $author->name, 
                $this->authors,
            ));
        }
    }
    
    public Author $mainAuthor {
        set (Author $mainAuthor) {
            $this->authors[] = $mainAuthor;
            $this->mainAuthor = $mainAuthor;
        }
        
        get => $this->mainAuthor;
    }
}

参考资料
[1]
属性钩子:https://wiki.php.net/rfc/property-hooks

[2]
get 和 set 操作分别声明可见性:https://wiki.php.net/rfc/asymmetric-visibility-v2

[3]
HTML5 解析支持:https://wiki.php.net/rfc/domdocument_html5_parser

[4]
DOM 规范合规性:https://wiki.php.net/rfc/opt_in_dom_spec_compliance

[5]
增强:https://wiki.php.net/rfc/dom_additions_84

[6]
array_find:https://www.php.net/array_find

[7]
array_find_key:https://www.php.net/array_find_key

[8]
array_any:https://www.php.net/array_any

[9]
array_all:https://www.php.net/array_all

[10]
bcdivmod:https://www.php.net/bcdivmod

[11]
bcround:https://www.php.net/array_find_key

[12]
bcceil:https://www.php.net/array_find_key

[13]
bcfloor:https://www.php.net/array_find_key

[14]
mb_trim:https://www.php.net/mb_trim

[15]
mb_ltrim:https://www.php.net/mb_ltrim

[16]
mb_rtrim:https://www.php.net/mb_rtrim

[17]
mb_ucfirst:https://www.php.net/mb_ucfirst

[18]
mb_lcfirst:https://www.php.net/mb_lcfirst

[19]
grapheme_str_split:https://www.php.net/grapheme_str_split

[20]
fpow:https://www.php.net/fpow

[21]
http_get_last_response_headers:https://www.php.net/http_get_last_response_headers

[22]
http_clear_last_response_headers:https://www.php.net/http_clear_last_response_headers

[23]
PDO 驱动特定子类:https://wiki.php.net/rfc/pdo_driver_specific_subclasses

[24]
IMAP、Pspell、OCI8 和 PDO_OCI8 扩展:https://wiki.php.net/rfc/unbundle_imap_pspell_oci8

[25]
PIE:https://qq52o.me/2845.html

[26]
RFC 提案:https://wiki.php.net/rfc/release_cycle_update

[27]
PHP 版本发布页面:https://www.php.net/releases/8.3/index.php

php直接给你支持Lazy Objects了,proxy的成本进一步降低。
按照symfony的节奏,symfony 8.x 应该最低就需要php8.4,那时候就可以大刀阔斧改造DI部分,往spring更靠拢了。

@guanhui07 guanhui07 added the php label Nov 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant