Welcome Guest, Not a member yet? Register   Sign In
Create a base Controller for my App problem
#1

[eluser]Stefano G[/eluser]
Hello,
I am a Java/J2EE developer, and I am new to CI (although I develop php4 apps since 2001).

My current problem is that, I wanted to code my super controllers and models that could provide some convenience methods to be used in the "child" classes (I think this is the very first thing an OOP developer wants to do before even start coding a new app!).

This is the situation I'd like to implement (using php4 and CI 1.5.4):

application/controller/appcontroller.php

Code:
class AppController extends Controller
      ...

application/controller/controller1.php

Code:
class Controller1 extends AppController
      ...

as simple as 1 2 3? not at all! I received the "can't inherit from non existing class..." error when invoking the Controller1 class from my URIs.

What's wrong with that? I have read that I can not extend Models but what about Controllers?

Thank you for any kind feedback.

Stefano
#2

[eluser]Clooner[/eluser]
Put this at the begin of the controller1 file and it should work

Code:
require_once("appcontroller.php");
#3

[eluser]coolfactor[/eluser]
Creating a class called MY_Controller that extends Controller is another option. MY_Controller would be loaded automatically for you by the system.

/application/libraries/MY_Controller.php
Code:
class MY_Controller extends Controller {

}

Then you just need to extend your normal controllers from MY_Controller instead of Controller to inherit the shared functionality.

(The "MY_" prefix can be anything you want, configurable in config/config.php)
#4

[eluser]Stefano G[/eluser]
Thanks for the replies Smile

@clooner: I have seen in some other discussion such a fix but I'd rather keep things clean to avoid future compatibility problems Wink

@coolfactor: I will try that solution but I really don't understand why my solution (that is the easiest one and absolutely OOP compliant) does not work... it looks very strange to me...

Thanks again

Stefano
#5

[eluser]Rick Jolly[/eluser]
[quote author="Stefano G" date="1189018020"]
...I have seen in some other discussion such a fix but I'd rather keep things clean to avoid future compatibility problems Wink

...I really don't understand why my solution (that is the easiest one and absolutely OOP compliant) does not work... it looks very strange to me...
[/quote]
How would php know where to find your class without including/requiring it? In java you would have to import classes, and php is no different. If you name your classes the same as their file names you could implement the php 5 __autoload() as a fallback. There is an example somewhere on the forums.
#6

[eluser]esra[/eluser]
I have not used hooks in this way before, but Codeigniter.php tests to check if any post_controller hooks exist prior to loading each controller. Could an Application controller be stored in libraries/ and would a post_controller hook handle loading the application controller class. It might be worth a try.

However if it does work, this would probably limit the application to a single application controller.

EDIT: The problem here is that the hooks would be called when loading application controller, so the only other solution I see would require a hack to Codeigniter.php.
#7

[eluser]Stefano G[/eluser]
Thanks everybody for the clarifying replies... I did it the "coolfactor-way" and I am quite happy with that Smile

[quote author="Rick Jolly" date="1189027715"]
How would php know where to find your class without including/requiring it? In java you would have to import classes, and php is no different.
[/quote]

I don't want to open any off-topic (java programming) discussion BUT, if I want to extend a class that is in the same package/directory of my subclass, I don't have to import anything Wink

Thanks.

Stefano
#8

[eluser]Rick Jolly[/eluser]
[quote author="Stefano G" date="1189083295"]
[quote author="Rick Jolly" date="1189027715"]
How would php know where to find your class without including/requiring it? In java you would have to import classes, and php is no different.
[/quote]

I don't want to open any off-topic (java programming) discussion BUT, if I want to extend a class that is in the same package/directory of my subclass, I don't have to import anything Wink

Thanks.

Stefano[/quote]

Ok, good point.

But php doesn't (and shouldn't) load all classes within the same directory (or child directories, or any other specified directories). Java can do it because it has application state. When the java server starts up, all those classes are loaded into memory. If php were to do it, then all those classes (most of which wouldn't be used), would have to be loaded on every request. So instead, we explicitly include/require just the classes we actually need.

However, the php 5 __autoload() function can do exactly what you need. You can write the __autoload() function to search for and load a class file, and it is only called when the class is instantiated. There are some nice code examples on php.net.
#9

[eluser]esra[/eluser]
[quote author="Rick Jolly" date="1189126144"]However, the php 5 __autoload() function can do exactly what you need. You can write the __autoload() function to search for and load a class file, and it is only called when the class is instantiated. There are some nice code examples on php.net.[/quote]

The __autoload method has been around since PHP3. Prior to version 5, most gurus recommended not using it because it might be removed in future releases. However, the PHP core team has been using it in PHP5 example code and Gutman's claimed it is here to stay in a interview some time ago. There are also a few other magic methods.

Could the class loader method in Loader.php be overloaded to use __autoload for fallback. I'm not sure if that method is private or not and can't check from this box.




Theme © iAndrew 2016 - Forum software by © MyBB