首先创建一个父类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如何在子类中调用父类的方法的详细内容,更多请关注亿速云其它相关文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。