Learning
Community
Open Exchange
Global Masters
InterSystems IRIS Data Platform 2019.4 / Application Development / First Look: ObjectScript / Try It: Storing and Retrieving Data Using ObjectScript / ObjectScript and Objects
Previous section   Next section

ObjectScript and Objects

You can use ObjectScript to create classes that have methods and properties. You can then instantiate objects of that class. The sample class FirstLook.Person.cls defines a class Person and then lets you create instances of that class, such as the person John Smith or the person Jane Doe.
The basic class definition looks like this:
Class FirstLook.Person Extends %Persistent
{

Property FirstName As %String [ Required ];

Property LastName As %String [ Required ];

}
The Person class extends the built-in InterSystems IRIS class %Persistent, which gives you access to some helpful methods, such as %New() and %Save(). Then the properties of the class are listed. In this case, you are simply storing a person’s first and last name.
Go ahead and create a new person using the Terminal. If you are not in the USER namespace, execute the following command:
set $namespace = "USER"
To create a new Person object, use the %New() method, which returns a “handle” to the new person, more formally known as an object reference, or OREF. Then set the new person’s properties and call the %Save() method to store the new person in the database.
USER>set person = ##class(FirstLook.Person).%New()

USER>set person.FirstName = "Sarah"
 
USER>set person.LastName = "Aarons"
 
USER>set status = person.%Save()

USER>write status
1
The %Save() method returns a status, which has the value 1 if it succeeds.
When you save an object, InterSystems IRIS stores it in a global for you automatically. The default global name is the class name with a D appended to the end, in this case ^FirstLook.PersonD.
If you display the contents of the global, you can see that the root node (the node without a subscript) holds an ID that is incremented for each new object that is stored. The remainder of the global is subscripted by ID. Each Person node contains a list of property values, where the list is denoted by $lb , for “list build.”
USER>zwrite ^FirstLook.PersonD
^FirstLook.PersonD=1
^FirstLook.PersonD(1)=$lb("","Sarah","Aarons")
You can also define instance methods, which operate on a specific instance, as opposed to class methods, which are generic to a class. For example, FirstLook.Person.cls contains a WriteName() method, which writes a person’s name.
/// Given an instance of a person, write person's name
Method WriteName() {
  write "The name of this person is:"
  write !, ..FirstName
  write !, ..LastName
}
An extra dot in front of a property or method name indicates the current object or class.
Since the variable person currently refers to Sarah Aarons, you can write her name like so:
USER>do person.WriteName()
The name of this person is:
Sarah
Aarons
As an exercise, create, store, and write a few new objects of the Person class, for example, Andrew Shaw, Peter Shaw, and Kate Aarons.
Previous section   Next section