Scope of variables when including files using php
I have a situation where I have 2 files, File A
and File B1
. I then have 20 other scri开发者_C百科pts including these files. File A contains some class that are instantiated in File B.
If a variable changes within a class in File A will it change for ALL 20 scripts?
I have a very weird issue where something is updating and when I put some debug code on each point where that variable is changed, it doesn't trigger. That is the reason for the question as it seems like one of the other 19 files including these is causing a change ...
To Clarify:
I am not including a file more than once. I have 20 different scripts running separately that include the SAME file (only do it once). This is so I only need to update 1 file when I make a change and all the other files use that source. If a public var is changed within a class in that ONE file, is it changed for all the 20 files.
THanks.
Yes.
An included file is included in whatever scope it is included in.
If you include a file in the topmost scope, then it has that scope. If you include a file via a function, then it's scope is within that function.
If you have 20 scripts that include "File A" every time in separates, so every instance will change his own "File A" class, what you can do is use include_once or require_once, then if you already include File A the next include for the same file will be not called, it useful if you already include file and you don't remember, now for the "Scope issue" if you create functionA inside functionB so only inside functionB you could use functionA it work the same for files, if you include fileA inside fileB and fileC inside fileB it will work greate, but if you do this include inside inner scope (like method, class) this file could not access anything out of the scope.
精彩评论