Cache_Lite_Function::call() -- Calls a cacheable function or method (or not if there is already a cache
for it)
Описание
call the given function with given arguments only if there is no cache about it ;
else, the output of the function is read from the cache then send to the browser and
the return value if read also from the cache and returned.
Возвращаемое значение
returns result of the function/method
Заметка
Эта функция не должна вызываться статически.
Пример
Пример 32-1. classical using with a function
<?php
require_once('Cache/Lite/Function.php');
$options = array(
'cacheDir' => '/tmp/',
'lifeTime' => 3600
);
$cache = new Cache_Lite_Function($options);
$cache->call('function_to_bench', 12, 45);
function function_to_bench($arg1, $arg2)
{
echo "This is the output of the function function_to_bench($arg1, $arg2) !<br>";
return "This is the result of the function function_to_bench($arg1, $arg2) !<br>";
}
?> |
|
Пример
Пример 32-2. classical using with a method
<?php
require_once('Cache/Lite/Function.php');
$options = array(
'cacheDir' => '/tmp/',
'lifeTime' => 3600
);
$cache = new Cache_Lite_Function($options);
$obj = new bench();
$obj->test = 666;
$cache->call('obj->method_to_bench', 12, 45);
class bench
{
var $test;
function method_to_bench($arg1, $arg2)
{
echo "\$obj->test = $this->test and this is the output of the method \$obj->method_to_bench($arg1, $arg2) !<br>";
return "\$obj->test = $this->test and this is the result of the method \$obj->method_to_bench($arg1, $arg2) !<br>";
}
}
?> |
|
If you try to use Cache_Lite_Function with $this object ($cache->call('this->method',...)
for example), have a look first at the last example of this page.
Пример
Пример 32-3. classical using with a static method
<?php
require_once('Cache/Lite/Function.php');
$options = array(
'cacheDir' => '/tmp/',
'lifeTime' => 3600
);
$cache = new Cache_Lite_Function($options);
$cache->call('bench::static_method_to_bench', 12, 45);
class bench
{
var $test;
function static_method_to_bench($arg1, $arg2) {
echo "This is the output of the function static_method_to_bench($arg1, $arg2) !<br>";
return "This is the result of the function static_method_to_bench($arg1, $arg2) !<br>";
}
}
?> |
|
Пример
Пример 32-4. another using with a method (how to use cache $this->method() calls)
<?php
// Since Cache_Lite-1.7.0beta2
require_once('Cache/Lite/Function.php');
$options = array(
'cacheDir' => '/tmp/',
'lifeTime' => 3600
);
$obj = new foo($options);
class foo
{
var $test;
function foo($options)
{
$cache = new Cache_Lite_Function($options);
$this->test = 'foobar';
$cache->call(array(&$this, 'method_bar'), 12, 45);
}
function method_bar($arg1, $arg2)
{
echo "\$this->test = $this->test and this is the output of the method \$this->method_bar($arg1, $arg2) !<br>";
return "\$this->test = $this->test and this is the result of the method \$this->method_bar($arg1, $arg2) !<br>";
}
}
?> |
|
So, for method calls, the best way is to use array(&$object, 'nameOfTheMethod') as first argument instead
of '$object->nameOfTheMethod' which doesn't work with "$this" for example.