CodeIgniter Forums
Extending the controller class: can you spot my error? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Extending the controller class: can you spot my error? (/showthread.php?tid=18766)



Extending the controller class: can you spot my error? - El Forum - 05-17-2009

[eluser]steward[/eluser]
1. Change the CI prefix:

system/application/config.php

Code:
$config['subclass_prefix'] = 'HANK_'; // 'MY_';


2. Extend the base controller

"Note: If you need to use a constructor in your class make sure you extend the parent constructor"

application/libraries/HANK_controller.php

Code:
class HankController extends Controller {

    public $foo;

    function HankController()
    {
        parent::Controller; // THIS IS THE ERROR LINE 19
    }
}
}

3. "If you are extending the Controller core class, then be sure to extend your new class in your application controller's constructors."

application/controllers/videos.php

Code:
class Videos extends HankController {

    function Videos()
    {
        parent::HankController();
    }

    function index()
    {
        echo $this->foo;
    }

Result:


Fatal error: Undefined class constant 'Controller' in C:\home\...\system\application\libraries\HANK_Controller.php on line 19



Extending the controller class: can you spot my error? - El Forum - 05-17-2009

[eluser]derekmichaeljohnson[/eluser]
Your class needs to be Hank_Controller not HankController.
Code:
class Hank_Controller extends Controller {

    public $foo;

    function Hank_Controller()
    {
        parent::Controller(); // THIS IS THE ERROR LINE 19
    }
}
}
Also:
Code:
class Videos extends Hank_Controller {

    function Videos()
    {
        parent::Hank_Controller();
    }

    function index()
    {
        echo $this->foo;
    }



Extending the controller class: can you spot my error? - El Forum - 05-17-2009

[eluser]Frank Berger[/eluser]
[quote author="steward" date="1242596988"]1. Change the CI prefix:
application/libraries/HANK_controller.php
Code:
class HankController extends Controller {

    public $foo;

    function HankController()
    {
        parent::Controller; // THIS IS THE ERROR LINE 19
    }
}
}

Fatal error: Undefined class constant 'Controller' in C:\home\...\system\application\libraries\HANK_Controller.php on line 19
[/quote]

As the error states, you're trying to call a constant, and it is true, you're calling a constant. What you want to do is calling a method, so change this:
Code:
function HankController()
    {
        parent::Controller; // THIS IS THE ERROR LINE 19
    }

to this:

Code:
function HankController()
    {
        parent::Controller(); // THIS IS THE ERROR LINE 19
    }

cheers Frank