D
D
dima00772017-09-18 17:31:17
Yii
dima0077, 2017-09-18 17:31:17

Parent::__construct for what?

can't understand for need parent::__construct

class ProductController extends Controller
{
    private $service;
 
    public function __construct($id, $module, ProductManageService $service, $config = [])
    {
        $this->service = $service;
        parent::__construct($id, $module, $config); //кому обращается parent
    }
 
    ...
 
    public function actionCreate()
    {
        $form = new ProductCreateForm();
 
        if ($form->load(Yii::$app->request->post()) && $form->validate()) {
            $id = $this->service->create($form);
            return $this->redirect(['view', 'id' => $id]);
        }
 
        return $this->render('create', [
            'model' => $form,
        ]);
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Kucherov, 2017-09-18
@DexterHD

parent refers to the Controller class from which you inherit.
It sets the controller id and whether the controller belongs to the module.
It also passes the configuration to the parent Component class
https://github.com/yiisoft/yii2/blob/f9fd4da8de6c0...

M
Maxim Timofeev, 2017-09-19
@webinar

a person needed a variable in the controller,
to be honest, I don’t understand why exactly __construct should be redefined, it seems to me that it’s more correct to do such things through behavior. But there are 3 options:
either it makes sense, but it is not visible from your example
, or the author knows php well and does not know yii,
or it was easier for the author and not .....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question