Question

I just started using CodeIgniter a few hours ago, and I've came across some problems. I am trying to call a function, which is currently in a controller named admin.php. I am trying to access this from a helper. How would I do this properly? I have tried almost everything, but nothing seems to be working as I'm receiving the following error:

Fatal error: Call to a member function login() on a non-object

Any help on this issue would be much appreciated

Was it helpful?

Solution

I agree with jimyi. Your helper and library classes should be independent of any particular controller in an application. If you have a "page title" helper, for example, it shouldn't depend on any particular behavior of your admin controller. What if you wanted to use the page helper in another application that didn't have an admin controller? Or what if you make changes to the admin controller down the road, now you have to make sure those changes don't break your helper.

If your helper function needs some particular bit of data that the admin controller has, you could pass it in as a function parameter from the controller. This way the helper is a dependency of the controller as opposed to the controller being a dependency of the helper.

Additionally, that error message means that whatever thing you are calling has not been instantiated properly. So you are probaly doing something like:

$this->load();

from within the helper. However, in the helper,

$this
is undefined (hence the 'non object' error). CodeIgniter provides a way to load models, libraries, and views from outside of a controller. But, as far as I know, it doesn't allow you to do this for controllers. That's fine though, for the reasons stated above. Gotta be careful when you introduce dependencies.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top