You are here: Re: include_once() within a function « PHP Programming Language « IT news, forums, messages
Re: include_once() within a function

Posted by Rik Wasmus on 09/08/07 20:12

On Sat, 08 Sep 2007 22:05:02 +0200, Jerry Stuckle =

<jstucklex@attglobal.net> wrote:

> Mikhail Kovalev wrote:
>> On 8 Sep, 21:12, 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 whe=
n
>>>>> 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 incl=
ude =

>>>> file return the variables
>>>> }
>>>> return $cache;
>>>> }
>>> return $cache?
>>> My function returns something else...
>>> Mmm, doesn't seem to work even with the return part.
>>>
>>> The functions from the included file are preserved, but the variable=
s
>>> which are declared in that file are lost. Ive tried declaring them
>>> static directly inside the included file, but with no luck.....
>>>
>>> Btw, the function in question here is called from another function..=
..
>> I guess what I'm looking for is how to make "once-included" variable=
s
>> global from inside the function....
>>
>
> That's exactly what Rik was telling you.
>
> Whether you include a file or not is immaterial. A static variable =

> within a function will keep its value between invocation; a non-static=
=

> variable will not.
>
> But the real question is - why does it take several seconds to load? =

> I've never had a PHP file take that long!

Well, I have, but normally that would only be so when waiting for outsid=
e =

(3rd party) resources to come trough, or scripts never ment for HTTP use=
=

but for cronjobs. Good point, there may be a lot to optimize there.
-- =

Rik Wasmus

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация