AFP documents are never executed directly. Rather, as already discussed in the section Cache above, each .afp document is converted to a FoxPro program and the latter compiled. Later requests will execute the compiled version only. .afp files are not the only ones for which allowance is made during compilation. In addition to the .afp the compilation will also include the files .code and .config.
All procedures and class definitions in the .code file as well as the converted .afp file are then merged in a common FoxPro program file. The program file thus generated also contains additional ancillary code that was auto-generated by AFP. The program file is basically a big DO CASE that will execute a range of actions that have been defined for an AFP page
One of these actions is the handling of the error event, another action is the execution of a page. Every CASE branch contains a mixture of auto-generated code and code written by you. Your code may even have been modified.
If the AFP document is assigned to a certain application, then that application will also be compiled; the procedures and classes in .afpa.code are transferred to the generated program file.
Events are procedures that always start with Event_ and whose names are tightly defined. Even if events are defined as procedures they will not run as procedures. Within the program flow of large generated program files certain points have been fixed. These are the points where event code is inserted. Thus, events turn into components of the entire program. In generated program files these point are always marked even if no event code has been inserted. Frequently, inspecting the programs in the cache lets you determine which event is most suitable for certain tasks. Events can be inserted at different points in the program if this makes it easier for AFP to execute it. One of these points can be found in the event InitVariables; the generated program uses it as follows:
If SERVER.nLevel == 1
If DOCUMENT.lLoadVariables
Local lcVariables, lnSelect
*( APP/PAGE.EVENT_LoadVariablesBefore
*)
lcVariables = APP.LoadVariables()
If Empty(m.lcVariables)
*( APP/PAGE.EVENT_InitVariables
*)
Else
lnSelect = Select()
Create Cursor ___AFPVariables (cVariables M)
Insert into ___AFPVariables values (m.lcVariables)
Release All like G*
Restore From Memo cVariables Additive
USE in ___AFPVariables
Select (m.lnSelect)
EndIf
Else
*( APP/PAGE.EVENT_InitVariables
*)
EndIf
EndIf