<p>I'm not a huge fan of it but for this situation it might be more appropriate to use <code>init()</code>, Looking at <code>Craft\BaseController</code>:</p>

<pre><code>/**
 * Initializes the controller.  This method is called by the Craft before the controller starts to execute.
 *
 * @throws HttpException
 * @return null
 */
public function init()
{

}
</code></pre>

<p>It seems a little odd to me to have a constructor for a controller though, what is it you're doing in it? <a href="http://www.yiiframework.com/doc/guide/1.1/en/basics.controller#filter">Yii's Controller filters</a> might be a better fit.</p>

CC BY-SA 3.0
2014-11-25T21:03:11.990Z
+1 for using `init()` here.
Brad Bell
CC BY-SA 3.0
2014-11-25T21:12:27.953Z
This sounds great. I was using __construct just because it's what I'm used to doing when extending a class. In many other languages or frameworks, it's required. I didn't know if the same was true here. Thanks for the heads up!
Gavin Anderegg
CC BY-SA 3.0
2014-11-25T20:38:47.067Z
2014-11-25T20:38:47.067Z
Mike Pepper
<p>\Craft\BaseController extends \CController so if you want to have a custom constructor, you should accept the same parameters and call the parent constructor with them.</p>

<pre><code>/**
 * @param string $id id of this controller
 * @param CWebModule $module the module that this controller belongs to.
 */
public function __construct($id, $module = null)
{
    parent::__construct($id, $module);
}
</code></pre>

CC BY-SA 3.0
2014-12-12T18:02:17.233Z
Thanks coffe4u. I saw this as well, but I didn't know how to get the $id for my controller as I was constructing it, and I didn't know what I was supposed to pass back as the $module. I think the correct answer is to not use a constructor, but the init() method as suggested above. It's Brad Bell approved, which makes me feel pretty good about it :)
Gavin Anderegg
CC BY-SA 3.0
2014-12-11T23:12:29.650Z
2014-12-11T23:12:29.650Z
coffe4u
<p>I'm implementing a custom controller as part of a plugin I'm building. There's some set up work that I'd like to do in the controller's constructor. I created a <code>__construct</code> method, and it seems to work as expected.</p>

<p><strong>My question:</strong> Is there some reason that I should call <code>parent::__construct()</code> inside of the constructor? It requires an argument, which looks like it should be an ID of some sort, but I'm not sure where I should get that.</p>

<plugin-development><controller>
Constructor in custom controller?
# Constructor in custom controller?

(Asked by: username_0 on 2014-11-25)

I'm implementing a custom controller as part of a plugin I'm building. There's some set up work that I'd like to do in the controller's constructor. I created a `__construct` method, and it seems to work as expected.

**My question:** Is there some reason that I should call `parent::__construct()` inside of the constructor? It requires an argument, which looks like it should be an ID of some sort, but I'm not sure where I should get that.

**Tags:** plugin-development, controller

---

## 2 answers

### Answer by username_1 on 2014-11-25. Score: 5 | Accepted answer

I'm not a huge fan of it but for this situation it might be more appropriate to use init(), Looking at Craft\BaseController:
/**
 * Initializes the controller.  This method is called by the Craft before the controller starts to execute.
 *
 * @throws HttpException
 * @return null
 */
public function init()
{

}

It seems a little odd to me to have a constructor for a controller though, what is it you're doing in it? Yii's Controller filters might be a better fit.

> **Comments:**
> **username_2**: +1 for using `init()` here.  
> **username_0**: This sounds great. I was using __construct just because it's what I'm used to doing when extending a class. In many other languages or frameworks, it's required. I didn't know if the same was true here. Thanks for the heads up!  

---

### Answer by username_3 on 2014-12-11. Score: 1

\Craft\BaseController extends \CController so if you want to have a custom constructor, you should accept the same parameters and call the parent constructor with them.
/**
 * @param string $id id of this controller
 * @param CWebModule $module the module that this controller belongs to.
 */
public function __construct($id, $module = null)
{
    parent::__construct($id, $module);
}

> **Comments:**
> **username_0**: Thanks coffe4u. I saw this as well, but I didn't know how to get the $id for my controller as I was constructing it, and I didn't know what I was supposed to pass back as the $module. I think the correct answer is to not use a constructor, but the init() method as suggested above. It's Brad Bell approved, which makes me feel pretty good about it :)  

---

