trying to run function just once |
I have a function that needs to run just once. I thought that setting $var=1 and using if (isset($var)){} would work but it doesn't. How can I run a function just one time in CI3?
proof that an old dog can learn new tricks
This should work for you, not tested.
PHP Code: protected static $runOnce = true; What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(06-21-2021, 08:44 PM)InsiteFX Wrote: This should work for you, not tested.Thanks. Where does protected static $runOnce = true; go? Can I put this in _init()?
proof that an old dog can learn new tricks
@richb201 ,
I have not used static before and decided to give it a try... discovered it ONLY works on Class Methods(...). The following KLUDGE should work which I have tried in: CI4 -> app/Common.php -> function fred(...) Code: //==================================================
If you are not inside a class context, you can use static variables instead.
PHP Code: static $hasRun;
Its amazing topic
function foo() { static $foo_called = false; if (!$foo_called) { $foo_called = true; // etc. } }
Enlightenment Is Freedom
(06-22-2021, 02:03 AM)paulbalandan Wrote: If you are not inside a class context, you can use static variables instead. (06-22-2021, 04:19 AM)paliz Wrote: Its amazing topicThe problem is that foo runs each time. (06-22-2021, 12:04 AM)John_Betong Wrote: @richb201 , Following your lead I stuck this in the bottom of _init() if(defined('RUNONCE')) { } else { $this->get_from_locker(); //bring down this guy's images define ('RUNONCE','YES'); } Unfortunately it keeps running get_from_locker() each time.
proof that an old dog can learn new tricks
Once per what? Once per day? Once per page load? What are you trying to accomplish? If you don't need to run it more than one time, in which context is it called repeatedly?
|
Welcome Guest, Not a member yet? Register Sign In |