It is tempting to spray include_once when multiple files require the same helper functions while they also depend on each other. "Lazy inclusion" may lead to circular dependency and, more importantly, a lack of clarity of the code execution flow.
Let's start with a basic LCHH setup where there is no need to include additional files:
When handling an update request, the showsub function is independently included by updatesub:
Now, let's introduce some library dependency to the basic case. Imagine that the result of showrec depends on libx but internally, it is showsub that needs the library.
The libx file in this case is mistakenly included on the parent level. Let's see how this decision eventually leads to an unnecessary include_once:
Conventional wisdom tells us that including a library at a "higher level" may benefit more execution paths. But when the sub view is called by the updater, which lacks the library context, libx must be included again. But including the same file twice leads to a redeclaration fatal error.
The syntactical solution is to use include_once on the sub view. A bigger crime is also often committed at this point by giving the main view an include_once.
There are two ways to properly structure this. If libx is only needed by the sub view, move the libx inclusion to showsub. This is a trivial case. In a project that is riddled with include_once everywhere, there could be many such trivial cases because at one point, the programmer gets lazy and stops scrutinizing the actual dependency structure. Everything is thrown into the kitchen sink.
There are times when both the main view and sub view require the same library. There can be additional sub views embedded in the same main view. In this case, libx should be included on the main level:
Now we replicate the library context with the updater. If the updater calls showrec, then nothing needs to be done because libx is already included. This is often the case with a tab-level loop back in Gyroscope.
If updatesub calls showsub, then we can include libx in updatesub:
Sometimes, showsub is called directly to update an LCHH container. In this case, we must create an indirect shell:
In practice, the "libx" might further include multiple files that are commonly grouped together:
Again, there is no "include_once". As a rule of thumb, if a problem seems to be solved by using include_once, don't take the easy way out. Try relocating the inclusion location first.