您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php如何在子类中调用父类的方法

2024/2/28 6:10:03发布22次查看
php在子类中调用父类的方法:
首先创建一个父类a,声明一个方法“test”
<?phpclass a{ public $a1='a1'; protected $a2='a2'; function test(){ echo "hello!<hr/>"; }}
然后创建子类b,用关键词“extends”继承父类a,创建子类b,使用“parent::test()”
<?phpclass 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如何在子类中调用父类的方法的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product