|
Posted by Rik Wasmus on 09/08/07 20:08
On Sat, 08 Sep 2007 21:12:23 +0200, Mikhail Kovalev =
<mikhail_kovalev@mail.ru> wrote:
> On 8 Sep, 20:49, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
>> On Sat, 08 Sep 2007 19:33:44 +0200, Mikhail Kovalev
>>
>> <mikhail_kova...@mail.ru> wrote:
>> > Hi all,
>> > I have a file which is to be included in another script and which
>> > takes several seconds to load(!), and which is actually not always
>> > used by the script, so instead of using include or include_once in =
the
>> > beginning, i'm using include_once() within a function, which is cal=
led
>> > on demand...
>> > The problem is the new data has the scope of the function, and when=
>> > the same function is called again, the data is lost....
>>
>> Well, you could assign it to static variable(s) in your function.
>>
>> function foo(){
>> static $cache;
>> if(!$cache){
>> $cache =3D include('/path/to/file'); //let the includ=
e =
>> file return the
>> variables
>> }
>> return $cache;
>>
>> }
>>
> return $cache?
> My function returns something else...
And what is it your function does too? Wouldn't it be better to create a=
=
function just serving the results of the include (which should NOT be =
argument dependant, or caching would be quite another problem), and doin=
g =
something with that variables in another?
> Mmm, doesn't seem to work even with the return part.
>
> The functions from the included file are preserved, but the variables
> which are declared in that file are lost. Ive tried declaring them
> static directly inside the included file, but with no luck.....
Just declaring them static with no check wether they are allready set =
would not have the desired effect.
> Btw, the function in question here is called from another function...
OK, an illustration.
<?php
//the function
function get_cached_include(){
static $data =3D array();
if(empty($data)) $data =3D include('/path/to/file');
return $data;
}
?>
<?php
//the included file
$bar =3D true;//boolean
$baz =3D array(1,5,6,7);//array
$foz =3D fopen('/path/to/another/file');
return compact('bar','baz','boz');
?>
<?php
//the real use
function some_functionality($args){
echo $args['bar'] ? 'bar is true':'bar is false';
foreach($args['baz'] as $baz){
echo "baz value =3D $baz\n";
}
echo is_resource($args['foz']) ? 'foz is a resource':'foz is not a =
resource';
}
//and let's do it:
some_functionality(get_cached_include());
?>
If this doesn't work like you want it to, you'll have to be more specifi=
c =
about your wishes and current best try. See this page about a more =
detailed explanation of static: =
<http://nl3.php.net/manual/en/language.variables.scope.php>
On a side note: a limited argument dependant cache in a function could b=
e =
created thus:
function cache_include_returns(){
static $cache =3D array();
$args =3D func_get_args();
$key =3D serialize($args); //which would mean $args _can_ be serialized=
, =
and aren't to 'big' to be usefull
if(!isset($cache[$key])) $cache[$key] =3D include('/path/to/file');//or=
=
possibly make the file dependant on the arguments...
return $cache[$key];
}
include file:
<?php
echo 'You just included '.__FILE__; //should only happen once per unique=
=
arguments
$return =3D "arguments were:";
foreach($args as $k =3D> $v){
$return .=3D "\n\t{$k} =3D> ".strval($v);
}
return $return;
?>
-- =
Rik Wasmus
[Back to original message]
|