-4

Reading over the documentation about include_once it basically says that the file in question won't be re-included if it's included already.

Can I equate this in my head as a static instance of the script, or will this lead me to trouble down the road?

EDIT

Not looking for a tutorial on include_once, I understand it quite well. Would like to know if I can conceptually attach the properties and characteristics of a static member, to this idea.

EDIT 2

Would the downvoters care to explain? It's a conceptual question, with a clear answer.

hakre
  • 193,403
  • 52
  • 435
  • 836
Ben
  • 54,723
  • 49
  • 178
  • 224
  • Once you've included it, any values and properties should persist in that stream. So if you are calling a function with a static value, and you include that script again later in the same stream, it won't re-include, so the static value should persist. When in doubt, poke the box. – Anthony May 03 '12 at 06:43
  • Uhh, so sounds like you're both saying different things. – Ben May 03 '12 at 06:44
  • @Anthony - you're understanding what I'm saying the most clearly, I think. Is it safe to think that way, or will I have some unforeseen crap happen later – Ben May 03 '12 at 06:44
  • It probably depends on what you mean by "static member", which I get fuzzy on real quick. But like I said, testing a simple scenario is easier than waiting for a definitive answer, which, when it comes to PHP, there might not be one. – Anthony May 03 '12 at 06:45
  • I mean what you said, pretty much. The script is accessed into memory at the time that the page is interpreted. If it's run again, then the original data is used, not a new instance. – Ben May 03 '12 at 06:46
  • You are comparing apples and oranges. No, it is not a static member. – Kaii May 03 '12 at 06:46
  • @Kali - As I said, static anything gets me loopy pretty fast. Is the argument that you can't call the include as a function and maintain a static value, or that you can't call the include in some other session and maintain the static value? – Anthony May 03 '12 at 06:48
  • @Kaii - I know it's not a static member. I'm not asking if it's a static member. – Ben May 03 '12 at 06:49
  • @Anthony - that's an example of what I'm talking about - cross session? recursive scripts? something I haven't foreseen? Is it really static-acting, or not so much? But I guess a simple scenario is all we can manage. I can do that without asking a question on SO. – Ben May 03 '12 at 06:49
  • 1
    @Steve - If the script is run again (from scratch, reload browser,etc) then all values are set to original. But if you include the script again later in the same compilation, it should maintain whatever value it left off at. – Anthony May 03 '12 at 06:50
  • @Steve - Updated. Double check with Kaii to confirm since they are much more confident on the topic of static-ness. – Anthony May 03 '12 at 06:57

1 Answers1

2

I'm not sure how the actual function works, but the best way to think about it is:

"If this file has been included/required earlier in this stream, no need to include/require it again."

The point is to avoid including a file that "redefines" a class or function because it was already defined in that same file included earlier.

The important thing to keep in mind is the idea of the script (and any included scripts along the way) as having a start and a finish. Any calls to include_once checks if that file has already been included since the start of the whole script. If it has, it doesn't bother to include it again, it just goes off of the originally included file.

Once the script is finished (no work left to be done, stream is closed, interrupted, aborted, etc), then re-starting the process treats the first include/include_once of a file as the first include, since it's the first time it's been included since the new running of the script. In that case, your back to square one, with all values being set back to default or unset.

Anthony
  • 36,459
  • 25
  • 97
  • 163
  • So, can't think of it as static, because there's no global context, it's only realised locally and won't persist. Thanks, helpful! – Ben May 03 '12 at 06:58
  • Look at the Pub/Sub techniques PHP's `SPL` library has introduced. There are ways to stash values to access cross-session, it just gets a bit more sophisticated. – Anthony May 03 '12 at 07:01
  • OK, cheers. Just the conceptual question for today. Didn't go too well but got a clear answer, finally. – Ben May 03 '12 at 07:04