时间:2021-07-01 10:21:17 帮助过:59人阅读
php访问父类方法的办法:可以用【$this->方法名();】来访问,如果子类中有该方法,则访问的是子类中的方法,如果子类中没有该方法,则访问父类中的方法。

方法的调用:
$this->方法名();如果子类中有该方法则调用的是子类中的方法,若没有则是调用父类中的。
parent::则始终调用的是父类中的方法。
(推荐教程:php视频教程)
变量的调用:$this->变量名;如果子类中有该变量则调用的是子类中的,若没有则调用的是父类中的
代码实现:
<?php
class A{
public $a1='a1';
protected $a2='a2';
function test(){
echo "hello!<hr/>";
}
}
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如何访问父类方法的详细内容,更多请关注gxlcms其它相关文章!