Not a direct CI question, how to call an object function from another object |
[eluser]atno[/eluser]
Hi, I have a question which is OO relative, i have an object login ( Code: class Login extends CI_Controller Code: public function is_logged_in(){} Code: class Memberarea extends CI_Controller Code: public function index(){ cheers
[eluser]Akinzekeel[/eluser]
Not sure if that's the best approach, but here's what I used to do in recent CI projects: - In application/core create a file MY_Controller.php with class MY_Controller extends CI_Controller - Put any universally accessible functions into this class - Instead of "class Memberarea extends CI_Controller" use "class Memberarea extends MY_Controller" - Now you can use $this->is_logged_in() without having to define this function multiple times
[eluser]atno[/eluser]
[quote author="sHiRoKKo1337" date="1304205769"]Not sure if that's the best approach, but here's what I used to do in recent CI projects: - In application/core create a file MY_Controller.php with class MY_Controller extends CI_Controller - Put any universally accessible functions into this class - Instead of "class Memberarea extends CI_Controller" use "class Memberarea extends MY_Controller" - Now you can use $this->is_logged_in() without having to define this function multiple times[/quote] So class Code: Login extends CI_Controller Code: class Login extends MY_Controller Code: MY_Controller
[eluser]Akinzekeel[/eluser]
Exactly, and put Code: public function is_logged_in() { /* your code here... */ } All controllers that extend from MY_Controller will inherit its functions and can access them with $this->...
[eluser]atno[/eluser]
Another way might be to create a custom library as mentioned here Creating Libraries and have all custom functions as libraries instead of using MY_Controller. Can someone confirm?
[eluser]Akinzekeel[/eluser]
Yes you can do that as well. Libraries have the advantage of being easily re-usable in other CI projects because you can simply copy & paste the library file. Sometimes I find the MY_Controller way a bit more comfortable, but it can be done with a library too. |
Welcome Guest, Not a member yet? Register Sign In |