Table of Contents
Previous Section
To store a variable's state in a WebObjects application, you declare it as global, session, or persistent.
The following section summarizes the different types of variables and their scope. For a comprehensive discussion of variables and scope, see the chapter Using WebScript.
The information in this section applies to WebScript except where it's specifically noted to apply to Objective-C.
Local Variable
- Where It's Declared
Inside a method in either an application or a component script.
- How You Declare It
id myVar;
- Where It's Visible
- Only inside the method in which it's declared.
- How Long It Lives
- For the duration of the method.
Transaction Variable
- Where It's Declared
Outside a method in a component script.
- How You Declare It
id myVar;
- Where It's Visible
- Inside the script in which it's declared.
- How Long It Lives
- For the duration of a transaction, which is defined as a request coming in and a response (usually an HTML page) going out.
Persistent Variable
- Where It's Declared
In WebScript, outside a method in a component script. In Objective-C, in the header file just like any other instance variable.
- How You Declare It
- In WebScript:
persistent myVar;
- In Objective-C: in a class that's subclassed from WOComponentController, declare a persistent variable just as you would any instance variable. Then implement the persistentKeys method. This method returns an array of the key names of the instance variables you want to make persistent.
- Where It's Visible
- In WebScript, inside the script in which it's declared.
- In Objective-C, inside the class in which it's declared.
- How Long It Lives
- For the duration of a session.
Session Variable
- Where It's Declared
In WebScript, outside a method in an application script. In Objective-C, in the header file just like any other instance variable.
- How You Declare It
- In WebScript:
session myVar;
- In Objective-C: in a class that's subclassed from WOWebScriptApplication, declare a session variable just as you would any instance variable. Then implement the sessionKeys method. This method returns an array of the key names of the instance variables for which you want to store state across a session.
- Where It's Visible
- In WebScript, inside the script in which it's declared. Component scripts can access session variables by messaging the application object. Each session has its own version of a session variable.
- In Objective-C, inside the class in which they're declared.
- How Long It Lives
- For the duration of a session.
Note: A session variable is identical to a persistent variable except that a persistent variable is scoped to the page in which it is declared.
Global Variable
- Where It's Declared
Outside a method in an application script.
- How You Declare It
id myVar;
- Where It's Visible
- In the application script. Component scripts can access global variables by messaging the application object. Every session sees global variables with the same value.
- How Long It Lives
- For the duration of the application.
Table of Contents
Next Section