Reputation: 2311
I have some controllers, where __constructor is similar in every of it. How to avoid copy-paste of similar code and write it in a one place?
Upvotes: 0
Views: 34
Reputation: 7111
Best place for ParentController.php would be application/core/ folder.
Upvotes: 0
Reputation: 2366
You can create a controller lets say ParentController
and extends it with base controller then add __contructor in that.
Now in all of your controllers where you want this constructor just extend your controllers with the created controller ParentController
.
ParentController.php:
class ParentController extends CI_Controller {
function __construct()
{
parent::__construct();
//your constructor code here..
}
}
Now the controllers in which you want the same constructor can be extended from ParentController : ClassA.php
class ClassA extends ParentController {
function __construct()
{
parent::__construct();
}
//your first controller
}
ClassB.php
class ClassB extends ParentController{
function __construct()
{
parent::__construct();
}
//Your second controller
}
Hope this helps.
Upvotes: 2