Welcome Guest, Not a member yet? Register   Sign In
trying to run function just once
#8

(This post was last modified: 06-22-2021, 02:16 PM by richb201.)

(06-22-2021, 02:03 AM)paulbalandan Wrote: If you are not inside a class context, you can use static variables instead.
PHP Code:
static $hasRun;

if (
$hasRun !== true)
{
    $hasRun true;

    // run your function here
}

I tried thisI tried placing $hasRun above class Configure extends CI_Controller but I get undefined variableThen I placed it between 
class Configure extends CI_Controller
{
    static $hasRun;    <<<<<<<<here
    
public function __construct()

And 
i still get undefined variable.
I put the if ($hasRun !== truein the __construct() 

(06-22-2021, 04:19 AM)paliz Wrote: Its amazing topic


function foo()
{
    static $foo_called = false;
    if (!$foo_called) {
        $foo_called = true;
        // etc.
    }
}
The problem is that foo runs each time.

(06-22-2021, 12:04 AM)John_Betong Wrote: @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:
//==================================================
function fred($val='Nothing set???', mixed $title='NO TITLE')
: string
{
  if( defined('CALL_ONCE_ONLY') ) :
    echo '<br> PREMATURE FUNCTION RETURN AND NOT USED';
    return '';
  endif;
  define('CALL_ONCE_ONLY', 'pretty please');


  $results = print_r($val, TRUE);

  echo $tmp = <<< ____EOT
    <div> <br> </div>
      <dl class="w88 mga fss bgl bd1 p42" >
        <dt> <b> $title </b> ==> </dt>
        <dd>
          <pre class="tal wrp bga">
            <b> $title </b> ==>
            <br>           
            $results
          </pre>
        </dd> 
      </dl> 
____EOT;

  return $result=(string) '';
}//

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
Reply


Messages In This Thread
trying to run function just once - by richb201 - 06-21-2021, 09:06 AM
RE: trying to run function just once - by ikesela - 06-21-2021, 07:10 PM
RE: trying to run function just once - by richb201 - 06-22-2021, 01:50 PM
RE: trying to run function just once - by paliz - 06-22-2021, 04:19 AM
RE: trying to run function just once - by craig - 06-23-2021, 03:26 AM
RE: trying to run function just once - by paliz - 06-24-2021, 02:37 AM



Theme © iAndrew 2016 - Forum software by © MyBB