php如何在子类中调用父类的方法-PHP问题

资源魔 28 0

php正在子类中挪用父类的办法:

起首创立一个父类A,申明一个办法“test”

<?php
class A{
    public $a1='a1';
    protected $a2='a2';
    function test(){
           echo "hello!<hr/>";
    }
}

而后创立子类B,用要害词“extends”承继父类A,创立子类B,应用“parent::test()”

<?php
class B extends A{//若A类以及B类没有正在同一文件中 请蕴含后(include)再操作
    public $a1='b1';
    function test2(){
            $this->test();
              parent::test();//子类挪用父类办法
    }
    function test()
    {   
        echo $this->a1.',';
        echo $this->a2.',';
        echo "b2_test_hello<hr/>";
    }
}
$a = new B();
$a->test();//b1,a2,b2_test_hello
$a->test2();//b1,a2,b2_test_hello//hello!

相干参考:资源魔

以上就是php若何正在子类中挪用父类的办法的具体内容,更多请存眷资源魔其它相干文章!

标签: php php教程 php故障解决 php使用问题

抱歉,评论功能暂时关闭!