亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PHP Reflection——這是一個錯誤還是預期的行為?

PHP Reflection——這是一個錯誤還是預期的行為?

PHP
慕田峪7331174 2023-09-22 17:03:32
我正在創建一個使用 ReflectionProperty 的類,但得到了奇怪的結果。本質上,調用是返回一些完全不相關的數組$reflectionProperty->getType()->getName()的值。它在看似隨機的條件下執行此操作。見下文:// this will be used as a type hinted property.class DummyClass {    public function __construct($arr) {}}// a class that sets its property values reflectivelyclass BaseClass {    /** @var ReflectionProperty[] */    private static $publicProps = [];    /**     * Gets public ReflectionProperties of the concrete class, and caches them     * so we do not need to perform reflection again for this concrete class.     *     * @return ReflectionProperty[]     * @throws ReflectionException     */    private function getPublicProps(){        if (!static::$publicProps) {            $concreteClass = get_class($this);            static::$publicProps = (new ReflectionClass($concreteClass))                ->getProperties(ReflectionProperty::IS_PUBLIC);        }        return static::$publicProps;    }請注意, 的值$propClass是abc123def456——這是怎么發生的?更多怪異將“abc123def456”的值更改為“12345678”即可。將“abc123def456”的值改為“123456789”,不起作用。省略 var_export(),它就會起作用。(不過,在其他情況下它仍然可能會損壞)。我的直覺告訴我這是一個 PHP 錯誤,但我可能做錯了什么,和/或這可能記錄在某處。我想要一些澄清,因為目前我唯一可靠的解決方案是不緩存反射的 $publicProps。ReflectionClass->getProperties()這會導致每次創建新的時都會進行不必要的調用ConcreteClass,我想避免這種情況。
查看完整描述

1 回答

?
猛跑小豬

TA貢獻1858條經驗 獲得超8個贊

事實證明這是 PHP 中的一個錯誤: https ://bugs.php.net/bug.php?id=79820

此處的最小復制: https: //3v4l.org/kchfm

7.4.9 中修復:https://www.php.net/ChangeLog-7.php#7.4.9


查看完整回答
反對 回復 2023-09-22
  • 1 回答
  • 0 關注
  • 119 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號