题目描述declare(strict_types=1);classstaticparent{static$parent_only;static$both_distinct;function__construct(){static::$parent_only='fromparent';static::$both_distinct='fromparent';//self::$parent_only='fromparent';//self::$both_distinct='fromparent';}}classstaticchildextendsstaticparent{static$child_only;static$both_distinct;function__construct(){static::$parent_only='fromchild';static::$both_distinct='fromchild';static::$child_only='fromchild';//self::$parent_only='fromchild';//self::$both_distinct='fromchild';//self::$child_only='fromchild';}}$a=newstaticparent;$a=newstaticchild;echo'Parent:parent_only=',staticparent::$parent_only,',both_distinct=',staticparent::$both_distinct,"
\r\n";echo'Child:parent_only=',staticchild::$parent_only,',both_distinct=',staticchild::$both_distinct,',child_only=',staticchild::$child_only,"
\r\n";?>题目来源及自己的思路输出Parent:parent_only=fromchild,both_distinct=fromparentChild:parent_only=fromchild,both_distinct=fromchild,child_only=fromchildhttps://www.php.net/manual/zh...相关代码//请把代码文本粘贴到下方(请勿用图片代替代码)$a=newstaticparent;var_dump(get_class_vars("staticparent"));$a=newstaticchild;echo"
";var_dump(get_class_vars("staticparent"));出现以下结果array(2){["parent_only"]=>string(10)"fromparent"["both_distinct"]=>string(10)"fromparent"}array(2){["parent_only"]=>string(9)"fromchild"["both_distinct"]=>string(10)"fromparent"}我想咨询下,为什么newstaticchild后父类的parent_only会被修改,而both_distinct却不会.另外输出的原因是什么,而且把注释里面self::打开,替换static,结果为何是一致Parent:parent_only=fromchild,both_distinct=fromparentChild:parent_only=fromchild,both_distinct=fromchild,child_only=fromchild
MMTTMM
慕田峪7331174
相关分类