PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
Copy Clear
Copy Format Clear
<?php class Once { static $echoed = []; private $key; public $output; function __construct( $file, $line, $output ) { $key = "$file:$line"; if ( static::$echoed[$key] === TRUE ) return; static::$echoed[$key] = FALSE; $this->key = $key; $this->output = $output; } public function echo() { if ( static::$echoed[$this->key] === TRUE ) return FALSE; static::$echoed[$this->key] = TRUE; echo $output; return TRUE; } } function echo_once( $file, $line, $output ) { global $echoed_once; if ( !isset($echoed_once) ) { echo "initializing \$echoed_once\n"; $echoed_once = []; } $key = "$file:$line"; if ( $echoed_once[$key] === TRUE ) return; $echoed_once[$key] = false; $echoer = function() { global $echoed_once; $echoed_once[$key] = true; echo $output; }; return $echoer; } $once = new Once( __FILE__, __LINE__, 'echoed'); $once->echo(); $once->echo();
Show:  
Copy Clear