ColdFusion Application.cfc - order of execution

前端 未结 5 582
旧巷少年郎
旧巷少年郎 2021-01-05 00:34

I need a reality check - and hopefully an explanation (if my reality is wrong).

The way the CF application framework evaluates things is this (my understanding) - re

5条回答
  •  遥遥无期
    2021-01-05 01:02

    There's two things at play here: when code runs, and when variable scopes are availed and how long they last.

    • The code outwith any method (ie: the "pseudoconstructor") runs every request. Obviously minimise the amount of code in this part of the CFC.
    • Code in various event handlers run as indicated by the event handler name, eg: onApplicationStart() code runs only once, when the application starts. Ditto onSessionStart() only runs once per new session.

    Scopes:

    • this scope is available throughout the CFC. Behaves exactly like the this-scope in any other CFC, except some this-scoped variables have special meaning (like this.name, this.datasource, etc). Those special-meaning variables can be changed per session or per request in the relevant handlers, but seem to apply to the entire system (ie: not for the specific session or request making the setting change). In normal CFCs, the this scope is used to expose public variables, but as there is no public instance of Application.cfc, there is no point using the this scope beyond making those special settings. If one wants to have variables available to all methods within the CFC, use the variables scope as one normally would. Contrary to someone's advice here, this-scoped variables are NOT the same as application-scoped variables.
    • The request scope is also available throughout the CFC (pseudo constructor and methods). These will also be available to the calling code of templates called later in the request, like any other request-scoped variables.
    • Application scope: not available in the pseudo-constructor, even after the this.name setting is made. Only becomes available in onApplicationStart() and thenceforth from there.
      • Session scope: similarly, not available in the pseudo-constructor or onApplicationStart()l and not until onSessionStart().

    I've demonstrated this in a blog post (test code provided), over here. it's too lengthy to include here, but the stuff above summarises it.

提交回复
热议问题