下面是带有未命名函数的PHP _构造
我在很大程度上遇到了这段代码下面是带有未命名函数的PHP _构造,php,function,laravel,oop,laravel-5,Php,Function,Laravel,Oop,Laravel 5,我在很大程度上遇到了这段代码 public function __construct( Request $request, UserRepository $userRepository, ProfileRepository $profileRepository ) { parent::__construct(); $this->request = $request; $this->profile_repository = $prof
public function __construct(
Request $request,
UserRepository $userRepository,
ProfileRepository $profileRepository
)
{
parent::__construct();
$this->request = $request;
$this->profile_repository = $profileRepository;
$this->model = $this->model_repository->getModel();
$this->entity_name_plural = str_plural($this->entity_name);
}
构造函数下面的未命名函数有什么用途?这是我第一次遇到这种情况,所以我不确定它到底是如何工作的。谢谢你的回复
parent::__construct();
此函数调用基本控制器的构造方法。这个函数的格式有点奇怪。它只是构造器,您可能可以更好地识别它的格式,如下所示:
public function __construct(Request $request, UserRepository $userRepository, ProfileRepository $profileRepository)
{
parent::__construct();
$this->request = $request;
$this->profile_repository = $profileRepository;
$this->model = $this->model_repository->getModel();
$this->entity_name_plural = str_plural($this->entity_name);
}
读取parent::_构造的行;正在调用父类的构造函数。构造函数下面的未命名函数有什么用途
您正在处理的类有一个父类,它从中继承了一些属性和行为方法。单独地当前类可能无法完成顺利执行程序所需的一些初始工作。和由于这种预初始化(有时是必要的),调用:
parent::__construct();
隐式地告诉父类:嘿,伙计!!!我将要创建我自己独特的对象,但首先;在我介入之前,我需要你为我清理路径并执行必要的初始化。。。。这基本上就是那部分所说和所做的。你的意思是什么?您只发布了一个_构造函数,它接受三个参数,尽管它们在多行上,然后执行一些操作。如果您指的是父调用,那就意味着对继承自的类调用构造函数。方法签名与方法体之间用一行空格分隔。没有未命名的函数。我明白了。谢谢我只是对它的格式感到困惑。没问题,我们是来帮忙的考虑接受一个答案,以确保将来的问题得到答案。谢谢你的详细解释。