为什么子类不能覆盖父类的“__debugInfo”?

Why isn't child class able to overwrite `__debugInfo` of parent class?

提问人:neubert 提问时间:5/23/2023 更新时间:5/23/2023 访问量:36

问:

这是我的代码:

use Carbon\Carbon;

class Demo extends Carbon
{
    public function __debugInfo(): array
    {
        return ['value' => $this->__toString()];
    }
}

$d = new Demo();
print_r($d);

我所期望的输出是这样的:

Demo Object
(
    [value] => 2023-05-23 13:57:18
)

但我得到的是:

Demo Object
(
    [endOfTime:protected] =>
    [startOfTime:protected] =>
    [constructedObjectId:protected] => 00000000000000020000000000000000
    [localMonthsOverflow:protected] =>
    [localYearsOverflow:protected] =>
    [localStrictModeEnabled:protected] =>
    [localHumanDiffOptions:protected] =>
    [localToStringFormat:protected] =>
    [localSerializer:protected] =>
    [localMacros:protected] =>
    [localGenericMacros:protected] =>
    [localFormatFunction:protected] =>
    [localTranslator:protected] =>
    [dumpProperties:protected] => Array
        (
            [0] => date
            [1] => timezone_type
            [2] => timezone
        )

    [dumpLocale:protected] =>
    [dumpDateProperties:protected] =>
    [date] => 2023-05-23 13:57:18.314578
    [timezone_type] => 3
    [timezone] => UTC
)

有什么想法吗?

php 对象 oop php-carbon

评论

0赞 Derek Pollard 5/23/2023
这很奇怪......如果取消返回类型(删除)会发生什么情况?: array
0赞 neubert 5/23/2023
@DerekPollard - 同样的事情。
1赞 Clint 5/24/2023
试试 var_dump($d) ?
0赞 KyleK 5/24/2023
__debugInfo仅适用于(对于任何对象,而不仅仅是子类或 Carbon 对象)var_dump()
0赞 neubert 5/24/2023
@KyleK - 你如何解释 3v4l.org/RCSW8

答: 暂无答案