|
Posted by Mikhail Kovalev on 09/08/07 21:18
On 8 Sep, 22:08, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
> On Sat, 08 Sep 2007 21:12:23 +0200, Mikhail Kovalev
>
>
>
> <mikhail_kova...@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 called
> >> > 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 = include('/path/to/file'); //let the include
> >> 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 doing
> 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 = array();
> if(empty($data)) $data = include('/path/to/file');
> return $data;}
>
> ?>
>
> <?php
> //the included file
> $bar = true;//boolean
> $baz = array(1,5,6,7);//array
> $foz = 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 = $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 specific
> 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 be
> created thus:
>
> function cache_include_returns(){
> static $cache = array();
> $args = func_get_args();
> $key = serialize($args); //which would mean $args _can_ be serialized,
> and aren't to 'big' to be usefull
> if(!isset($cache[$key])) $cache[$key] = 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 = "arguments were:";
> foreach($args as $k => $v){
> $return .= "\n\t{$k} => ".strval($v);}
>
> return $return;
> ?>
>
> --
> Rik Wasmus
I think I grasped the main portion of your example, but the approach
is completely infamiliar to me.
Heres my situation:
main file:
<?php
function1($arg1) {
include_once('included_file.php');
return function_from_included_file_3($arg1, var1, var2, var3); //
var1,2,3 see below
}
function2($arg1) {
if then else etc
return function1($arg1);
}
?>
included file:
<?php
function_from_included_file_1...
function_from_included_file_2...
function_from_included_file_3...
var1
var2
var3...
?>
my problem is that i fail to make var1, var2, var3 static, when the
second time function1 from main file is called
Navigation:
[Reply to this message]
|