加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 重写方法被调用两次

发布时间:2020-12-13 17:30:21 所属栏目:PHP教程 来源:网络整理
导读:我有一个代码片段 ?phpabstract class Testing{ public abstract function tester(); public function testing(){ $this-tester(); }}class Test extends Testing{ public function tester(){ echo 'test'; }}$t = new Test();$t-testing(); 我应该有一个输
我有一个代码片段

<?php

abstract class Testing{
    public abstract function tester();

    public function testing(){
        $this->tester();
    }
}

class Test extends Testing{
    public function tester(){
        echo 'test';
    }
}

$t = new Test();
$t->testing();

我应该有一个输出测试,但我得到的输出是testtest?
为什么测试器()被调用两次?

参考link to ideone

解决方法

PHP脚本语言不区分大小写. (虽然不适用于变量)

由于您的子类没有任何构造函数,因此父类构造函数会被触发.

当你这样做..

$t = new Test();

父类构造函数被触发,这是public function testing(),(参见类匹配的名称)

PHP Docs ..

For backwards compatibility,if PHP 5 cannot find a __construct() function for a given class,and the class did not inherit one from a parent class,it will search for the old-style constructor function,by the name of the class.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读