Welcome Guest, Not a member yet? Register   Sign In
calling class within the controllers directory
#1

[eluser]rudal[/eluser]
Is it possible to call a class->function from the controllers directory?
#2

[eluser]Dam1an[/eluser]
If the function is in this controller, you can call $this->function
If it's in a library, you need to either use the CI loader and then use $this->library->function
Or you can include it yourself and create an object and then call it like $my_object->function

Does that clear things up a bit?
#3

[eluser]rudal[/eluser]
Sorry, i wasnt clear earlier.

Let's say I have a controller file called "license.php" within the file:

class License extend Controler {
function { ... }
function list($param1) { .... }
}

Then i have another controller file called "main.php". In this file, main.php, how can I call the function list() within the license.php file? Is it possible?

Thanks so much.
#4

[eluser]n0xie[/eluser]
Using the normal CI install you can't call a controller from another controller. There are adaptations which make this possible. If you have a function you want to share among controllers, it's better to either put them in a model, library or helper. This will make code reusability much simpler.
#5

[eluser]Dam1an[/eluser]
You will need to include the Licence controller
Code:
include(APPPATH.'controllers.license.php)
You will (probably) have to then create an object for the License controller in order to call the methods (unless it's static)
Personally, I've never done that

Depending on the logic in the list function, it might be better suited as a library, so you can call it from anywhere, or if you need that function in several controllers, you could make the main controller extend the license controller
#6

[eluser]rudal[/eluser]
yeah i got used too much on writing everything as controllers and now so much duplicates that i need to do. :\ .... i guess i need to move on with the libraries.

Thank you so much for the advice, guys. really appreciate it Smile
#7

[eluser]wiredesignz[/eluser]
Extending controller classes is more appropriate than creating libraries.




Theme © iAndrew 2016 - Forum software by © MyBB