gpt4 book ai didi

php - 在子 PHP 之后自动调用父构造函数

转载 作者:可可西里 更新时间:2023-10-31 22:18:51 26 4
gpt4 key购买 nike

我尝试使用父类的名称(如构造函数)并为我部分工作。

第一次调用

"DarthVader method"

像构造函数但从不调用

"LukeSkywalker constructor"..

有人知道怎么做吗?

example:

达斯.php

class DarthVader{
public function DarthVader(){
echo "-- Obi-Wan never told you what happened to your father.\n";
}
public function reponse(){
echo "-- No. I am your father\n";
}
}

卢克.php

include("Darth.php")

class LukeSkywalker extends DarthVader{
public function __constructor(){
echo "- He told me enough! He told me you killed him!\n"
$this->response();
}
}

Expected result:

  • 欧比旺从未告诉过你父亲的遭遇。

  • 他告诉我够多了!他告诉我你杀了他!

  • 没有。我是你爸爸

我真的希望它自动如此。

最佳答案

根据文档:http://php.net/manual/en/language.oop5.decon.php

Note: Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct() within the child constructor is required. If the child does not define a constructor then it may be inherited from the parent class just like a normal class method (if it was not declared as private).

关于php - 在子 PHP 之后自动调用父构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16469486/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com