Defining a lazy function in PHP - is this possible?

In JavaScript, you can use Lazy Function Definitions to optimize the second function call by performing expensive one-time operations only on the first function call.

I would like to do the same in PHP 5, but function overriding is not allowed and does not overload the function.

In fact, what I would like to do is as follows: it’s only optimized, so calls of the 2nd number (for example, 25-100) do not need to be checked again if they are the first call.

$called = false;
function foo($param_1){
  global $called;
  if($called == false){
    doExpensiveStuff($param_1);
    $called = true;
  }
  echo '<b>'.$param_1.'</b>';
}

PS I was thinking about using include_once () or require_once () as the first line of a function to execute external code only once, but I heard that they are also expensive.

? ?

+5
8

var:

function foo() {
    static $called = false;
    if ($called == false) {
        $called = true;
        expensive_stuff();
    }
}

. . , , , , , , .

+13

? , - .

+6

.

if( !function_exists('baz') )
{ 
    function baz( $args ){ 
        echo $args; 
    }
}

, .

create_function, DONT, , , free() 'd php eval().

, PHP5.3, "" http://wiki.php.net/rfc/closures

if( !isset( $baz ) ) 
 { 
    $baz = function( $args )
    { 
        echo $args;
    }
}

$baz('hello');

$baz = function( $args )
{ 
       echo $args + "world"; 
}
$baz('hello');

, .

$fname = 'f_first'; 
function f_first( $even ) 
{ 
    global $fname; 
    doExpensiveStuff(); 
    $fname = 'f_others';
    $fname( $even );
    /* code */ 
}
function f_others( $odd ) 
{
     print "<b>".$odd."</b>";
}

foreach( $blah as $i=>$v ) 
{
   $fname($v);
}

, , .

PHP5.3 :

$func = function( $x ) use ( $func ) 
{ 
     doexpensive(); 
     $func = function( $y )
     { 
          print "<b>".$y."</b>";
     }
     $func($x);
}
foreach( range(1..200) as $i=>$v ) 
{ 
    $func( $v ); 
}

( , , , .;))

,

$data = // some array structure
doslowthing(); 
foreach( $data as $i => $v ) 
{
   // code here 
}

, , . , , :)

+4

, include() include_once(), , include(). , . require_once().

+1

, , :

$func = "foo";    

function foo()
{
    global $func;
    $func = "bar";
    echo "expensive stuff";
};


function bar()
{
    echo "do nothing, i guess";
};

for($i=0; $i<5; $i++)
{
    $func();
}

0

, ? , PHP . , .

Class SomeClass{
    protected $whatever_called;
    function __construct(){
        $this->called = false;
    }
    public function whatever(){
        if(!$this->whatever_called){
            //expensive stuff
            $this->whatever_called = true;
        }
        //rest of the function
    }
} 

, , . , static.

0

PHP , , , . PHP , .

javascript :

var cache = null;
function doStuff() {
  if (cache == null) {
    cache = doExpensiveStuff();
  }
  return cache;
}

( PHP) :

class StuffDoer {
  function doStuff() {
    if ($this->cache == null) {
      $this->cache = $this->doExpensiveStuff();
    }
    return $this->cache;
  }
}

, oop , , .

, , PHP 5.3, , /, , , . . PHP rfc-wiki .

0

?

function doStuff($param1) {
    static $called = false;
    if (!$called) {
        doExpensiveStuff($param1);
        $called = true;
    }
    // do the rest
}

, :

function doStuff($param1) {
    static $buffer = array();
    if (!array_key_exists($param1, $buffer)) {
        doExpensiveStuff($param1);
        $buffer[$param1] = true;
    }
    // do the rest
}

. .

0

All Articles