Welcome Guest, Not a member yet? Register   Sign In
'extends Controller' - very very strange problem!!
#1

[eluser]OPi[/eluser]
Hello everyone,
I created W4A_Controller and W4A_Controller2 that extends the Controller class:
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class W4A_Controller extends Controller
{
    function W4A_Controller()
    {
        parent::Controller();
    }
}
/* Location: ./system/application/libraries/W4A_Controller.php */
?>

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class W4A_Controller2 extends Controller
{
    function W4A_Controller2()
    {
        parent::Controller();
    }
}
/* Location: ./system/application/libraries/W4A_Controller2.php */
?>

The class 'Demo' extends 'W4A_Controller' and working! BUT if the class 'Demo' extends 'W4A_Controller2' NOT working:
--&gt; Fatal error: Class 'W4A_Controller2' not found in <my_path>system\application\controllers\demo.php on line 2
Code:
&lt;?php
class Demo extends [color=red]W4A_Controller[/color]
{
    function Demo()
    {
        parent::[color=red]W4A_Controller[/color]();
    }

    function index()
    {
        echo 'hello!';
    }
}
/* Location: ./system/application/controllers/Demo.php */
?&gt;
Code:
&lt;?php
class Demo extends [color=blue]W4A_Controller2[/color]
{
    function Demo()
    {
        parent::[color=blue]W4A_Controller2[/color]();
    }

    function index()
    {
        echo 'hello!';
    }
}
/* Location: ./system/application/controllers/Demo.php */
?&gt;

I see that the problem is not the number 2 in W4A_Controller2
(I use CI 1.7.2, apache 2.2.16, php 5.2.14)

Any ideas? thanks
#2

[eluser]WanWizard[/eluser]
How do you load the W4A_Controller and W4A_Controller2 in your demo example?

Is "W4A_" defined in config/config.php as subclass_prefix?

If so, W4A_Controller is loaded automatically by CI when Controller is loaded. This doesn't happen with W4A_Controller2.
#3

[eluser]OPi[/eluser]
Yes, “W4A_” is defined in config/config.php as subclass_prefix.

I read the User Guide that the class W4A_Controller is read automatically puts it into the folder 'application/libraries' I thought the same for W4A_Controller2 was in the same folder but I'm wrong ..
If I understand correctly, 'CI' needs to read the library 'W4A_Controller2'.. Is this right?

Thanks a lot!
#4

[eluser]WanWizard[/eluser]
Correct.

When you load a controller, CI will automatically load the Controller class from the system directory, and checks if a {subclass_prefix}controller exists in the application library directory. If so, it loads that automatically too.

That obviously doesn't work with a controller named differenly. You might want to read Phil Sturgeon's excellent article on Base classes.
#5

[eluser]OPi[/eluser]
Thanks are nice, the article is very useful!
I really like CI and want to learn to use it well.

Oscar




Theme © iAndrew 2016 - Forum software by © MyBB