Basic Contents of a Class Definition
Basic Contents of a Class Definition
An InterSystems IRIS class definition can include the following items, all known as class members:
-
Parameters — A parameter defines a constant value for use by this class. The value is set at compilation time.
-
Properties — A property contains data for an instance of the class.
-
Methods — There are two kinds of methods: instance methods and class methods (called static methods in other languages).
-
Class queries — A class query defines an SQL query that can be used by the class and specifies a class to use as a container for the query. See Defining and Using Class Queries.
-
XData blocks — An XData block is a unit of data (XML, JSON, or YAML), for use by the class. See Defining and Using XData Blocks.
-
Other kinds of class members that are relevant only for persistent classes.
A class definition can include keywords; these affect the behavior of the class compiler. You can specify some keywords for the entire class, and others for specific class members. These keywords affect the code that the class compiler generates and thus control the behavior of the class.
The following shows a simple class definition with methods written in ObjectScript and in Python:
Class MyApp.Main.SampleClass Extends %RegisteredObject
{
Parameter CONSTANTMESSAGE [Internal] = "Hello world!" ;
Property VariableMessage As %String [ InitialExpression = "How are you?"];
Property MessageCount As %Numeric [Required];
ClassMethod HelloWorld() As %String [ Language = objectscript ]
{
Set x=..#CONSTANTMESSAGE
Return x
}
Method WriteIt() [ Language = objectscript, ServerOnly = 1]
{
Set count=..MessageCount
For i=1:1:count {
Write !,..#CONSTANTMESSAGE," ",..VariableMessage
}
}
}
Class MyApp.Main.SampleClass Extends %RegisteredObject
{
Parameter CONSTANTMESSAGE [Internal] = "Hello world!" ;
Property VariableMessage As %String [ InitialExpression = "How are you?"];
Property MessageCount As %Numeric [Required];
ClassMethod HelloWorld() As %String [ Language = python ]
{
import iris
x = iris.MyApp.Main.SampleClass._GetParameter("CONSTANTMESSAGE")
return x
}
Method WriteIt() [ ServerOnly = 1, Language = python ]
{
import iris
count = self.MessageCount
print()
for i in range(count):
print(iris.cls(__name__)._GetParameter("CONSTANTMESSAGE"), self.VariableMessage)
}
}
Note the following points:
-
The first line gives the name of the class. MyApp.Main.SampleClass is the full class name, MyApp.Main is the package name, and SampleClass is the short class name.
Your IDE and other user interfaces treat each package as a folder.
-
Extends is a compiler keyword.
The Extends keyword specifies that this class is a subclass of %RegisteredObjectOpens in a new tab, which is a system class provided for object support. This example class extends only one class, but it is possible to extend multiple other classes. Those classes, in turn, can extend other classes.
-
CONSTANTMESSAGE is a parameter. By convention, all parameters in InterSystems IRIS system classes have names in all capitals. This is a convenient convention, but you are not required to follow it.
The Internal keyword is a compiler keyword. It marks this parameter as internal, which suppresses it from display in the class documentation. This parameter has a string value.
To access class parameters via Python, use the built-in method %GetParameter() to return the value of the parameter. However, in Python the percent sign character is not permitted in identifier names, so you need to substitute an underscore, instead.
-
To refer to the current class in ObjectScript, use a dot (.).
-
To refer to the current class in Python, you can use self, iris.Package.Class, or iris.cls(__name__), depending on the context. The example shows all three syntax forms. Note that iris.cls(__name__) has two underscores before and after name. (For more details, see References to Other Class Members.)
-
VariableMessage and MessageCount are properties. The item after As indicates the types for these properties. InitialExpression and Required are compiler keywords.
You can access an InterSystems IRIS class property directly from ObjectScript or Python, as in the example.
-
HelloWorld() is a class method and it returns a string; this is indicated by the item after As.
This method uses the value of the class parameter.
-
WriteIt() is an instance method and it does not return a value.
This method uses the value of the class parameter and values of two properties.
The ServerOnly compiler keyword means that this method will not be projected to external clients.
The following Terminal session shows how we can use this class. Both terminal shells are valid for the ObjectScript and Python versions of the class.
TESTNAMESPACE>write ##class(MyApp.Main.SampleClass).HelloWorld()
Hello world!
TESTNAMESPACE>set x = ##class(MyApp.Main.SampleClass).%New()
TESTNAMESPACE>set x.MessageCount=3
TESTNAMESPACE>do x.WriteIt()
Hello world! How are you?
Hello world! How are you?
Hello world! How are you?
>>> print(iris.MyApp.Main.SampleClass.HelloWorld())
Hello world!
>>> x = iris.MyApp.Main.SampleClass._New()
>>> x.MessageCount=3
>>> x.WriteIt()
Hello world! How are you?
Hello world! How are you?
Hello world! How are you?
Effective with InterSystems IRIS 2024.2, an optional shorter syntax for referring to an InterSystems IRIS class from Embedded Python has been introduced. Either the new form or the traditional form are permitted.