简体   繁体   English

从子类构造函数中访问父类方法

[英]Access parent class methods from within child class constructor

I am using Laravel 5.3. 我正在使用Laravel 5.3。 In my form request which extends the request class I am trying to access method from the parent class but it is throwing an error and I can't seem to see why. 在扩展请求类的表单请求中,我尝试从父类访问方法,但是它引发了错误,我似乎看不出为什么。 My form request constructor looks like the following. 我的表单请求构造函数如下所示。 Am I missing something here? 我在这里想念什么吗?

The call works when I put it in other methods, just not from the constructor where it ideally needs to be. 当我将其放在其他方法中时,调用将起作用,而不仅仅是在理想情况下从构造函数中调用。

Accessing the parent below triggers "Fatal error: Call to a member function get() on null in vendor\\laravel\\framework\\src\\Illuminate\\Http\\Request.php:601" 在下面访问父级将触发“致命错误:在vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Http \\ Request.php:601中对null成员函数get()的调用”

    protected $test= [];

    public function __construct(myRepositoryInterface $myRepository) {

        $this->myRepository= $myRepository;
        if( parent::has('someName') ){
           $this->test= $myRepository->someMethod(parent::input('someName'));
       }
   }

There are a few problems. 有一些问题。

First, Laravel's Form Request class is a subclass of Symfony's Request class. 首先,Laravel的Form Request类是Symfony的Request类的子类。 If you take a look at that class, it has this constructor: 如果您看一下该类,则它具有以下构造函数:

public function __construct(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null)
{
    $this->initialize($query, $request, $attributes, $cookies, $files, $server, $content);
}

You created your own constructor and changed the behavior of the Request class, essentially breaking it. 您创建了自己的构造函数,并更改了Request类的行为,从本质上破坏了它。 You aren't accepting any of the parameters that the superclass requires. 您不接受超类所需的任何参数。

The Call to a member function get() on null gives us a clue. Call to a member function get() on nullCall to a member function get() on null提供了一个线索。 This is the function where get() is being called: 这是调用get()的函数:

protected function retrieveItem($source, $key, $default)
{
    if (is_null($key)) {
        return $this->$source->all();
    }

    return $this->$source->get($key, $default);
}

Your $source is null. 您的$source为空。 The $source can be something like the headers property, which is null because of your new constructor. $source可以类似于headers属性,由于您使用了新的构造函数,因此该属性为null。 The error has nothing to do with the parent class' has method and everything to do with the constructor. 该错误与父类的has方法无关,与构造函数无关。

Second, rather than calling parent::has() , you should be calling $this->has() . 其次,您应该调用$this->has()而不是调用parent::has() $this->has()

Finally, I would take that out of the constructor. 最后,我将其从构造函数中删除。 Initialize your repository somewhere else. 在其他地方初始化您的存储库。 If you do need this to be in your constructor for some reason, try accepting all other parameters and then your repository. 如果出于某种原因确实需要将其放入构造函数中,请尝试接受所有其他参数,然后再接受存储库。 Remember to call parent::__construct(...parameters... ) and run your own logic. 记住要调用parent::__construct(...parameters... )并运行自己的逻辑。

您可能会忘记在子构造函数中调用parent :: __ construct()吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM