Welcome Guest, Not a member yet? Register   Sign In
Extending the controller class without MY_
#1

[eluser]john36122340[/eluser]
Hi everyone,

I am trying unsuccessfully to extend the base controller class without using the MY_ prefix.

I created a new library called 'base_controller.php' and saved it in the 'application/libraries' folder:

Code:
<?
class base_controller extends Controller
{

    protected $name = 'Joe';
    
    public function __construct()
    {
        parent::__construct();
        echo 'Base controller loaded.';
    }
    
    public function hello($name)
    {
        return "Your name is $name.";
    }
    
}
?>

This library is then autoloaded in the main autoload.php file:
Code:
$autoload['libraries'] = array(
    'database',
    'parser',
    'base_controller'
);

In the controller I am working with I have:
Code:
<?
class child_controller extends base_controller
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function print_something()
    {
        echo "Name is $this->name.";
        
        echo '<hr />';
        
        echo $this->base_controller->hello('Paul');
    }
    
}

?&gt;

I keep on getting the error:

Fatal error: Class 'base_controller' not found in ...

Does anyone have any ideas on this?

Thanks,

John
#2

[eluser]danmontgomery[/eluser]
You have to include the file the class is in or write an __autoload method.
#3

[eluser]jedd[/eluser]
Hi John and welcome to the CI forums.

[quote author="john36122340" date="1268278265"]
I am trying unsuccessfully to extend the base controller class without using the MY_ prefix.
[/quote]

Why?

Alternatively ..

Why not just change $config['subclass_prefix'] in your config/config.php file to be set to "base_"?
#4

[eluser]john36122340[/eluser]
Hi guys,

Thanks for the replies and thanks for the warm welcome. :-)
Only being playing with CodeIgniter for a day or so and I must say it seems incredible...

I don't want to change the $config[‘subclass_prefix’] because there are going to be multiple files that entend the main controller in different ways depending on the section being visited and I intend extending the core classes later.

The 'autoload' idea is a good one, I didn't think it would be necessary though, I thought the autload section in the config file did this???

If I have to use an autoload function to do this, what would be the best way, a hook or is there any examples that other folks have done.

Thanks,

John
#5

[eluser]jedd[/eluser]
Hi John,

[quote author="john36122340" date="1268280854"]
I don't want to change the $config[‘subclass_prefix’] because there are going to be multiple files that entend the main controller in different ways depending on the section being visited and I intend extending the core classes later.
[/quote]

Think carefully about this. It can get very confusing if you start having lots of classes that you're extending from / lots of levels of ancestry of classes.

I think(!) one way of faking what you're trying to do would be to have a MY_Controller (or base_Controller, if you changed the subclass prefix) file that contains all the various classes that you want to initialise. I think this is an ugly approach - in part from a design point of view, and in part because you're fooling the framework into loading multiple classes by effectively autoloading what it thinks is one class.

I'd look at other options here - libraries, helpers, or moving functionality into your models, etc.

Depends on the scale and nature of the stuff you're wanting to implement - perhaps you could throw out some hints on the kinds of thing you're trying to do here.
#6

[eluser]danmontgomery[/eluser]
[quote author="john36122340" date="1268280854"]The 'autoload' idea is a good one, I didn't think it would be necessary though, I thought the autload section in the config file did this???

If I have to use an autoload function to do this, what would be the best way, a hook or is there any examples that other folks have done.

Thanks,

John[/quote]

You're talking about CodeIgniter's autoload functionality, which does do this if you use the subclass_prefix as suggested.

I was talking about PHP's __autoload functionality.
#7

[eluser]john36122340[/eluser]
Hi guys,

Thanks for the replies...

I'd agree about not using MY_Controller to load multiple classes, it might work but like you said loading multiple classes instead of one could be a bit messy.

I don't have any real code, not really trying to do anything, just testing CodeIgniter and have ideas about how to extend the default controller in different ways depending on the page...

OK I think I got it:

Are these 2 statements correct???

If I use the 'subclass prefix' at the start of the file name it will be autoloaded automatically?

1.) This method needs no autoloads or requires (where 'MY_' is the name of the subclass_prefix):

Code:
class Controller_Name extends MY_Controller
{

}

2.) This method requires an PHP autoload function or a require('path_to_foo_controller.php');
It is NOT available by just placing it in $autoload['libraries']?

Code:
class Controller_Name extends Foo_Controller
{

}

Thanks for the heads up,

John




Theme © iAndrew 2016 - Forum software by © MyBB