Skip to main content

Basic Features of Object Classes

Basic Features of Object Classes

With the object classes, you can perform the following tasks, among others:

  • You can create an object (an instance of a class). To do so, you use the %New() method of that class, which it inherits from %RegisteredObjectOpens in a new tab.

    For example:

     set myobj=##class(Sample.Person).%New()
    myobj = iris.cls("Sample.Person")._New()
    

    Python method names cannot include a percent sign (%). You can call any ObjectScript method that contains the % character from Python by replacing it with an underscore (_), as in the example.

  • You can use properties.

    You can define properties in any class, but they are useful only in object classes, because only these classes enable you to create instances.

    Any property contains a single literal value, an object (possibly a collection object), or a multidimensional array (rare). The following example shows the definition of an object-valued property:

    Property Home As Sample.Address;

    Sample.Address is another class. The following shows one way to set the value of the Home property:

     Set myaddress=##class(Sample.Address).%New()
     Set myaddress.City="Louisville"
     Set myaddress.Street="15 Winding Way"
     Set myaddress.State="Georgia"
    
     Set myperson=##class(Sample.Person).%New()
     Set myperson.Home=myaddress
    
    import iris
    myaddress=iris.cls("Sample.Address")._New()
    myaddress.City="Louisville"
    myaddress.Street="15 Winding Way"
    myaddress.State="Georgia"
    
    myperson=iris.cls("Sample.Person")._New()
    myperson.Home=myaddress
    
  • You can invoke methods of an instance of the class, if the class or its superclasses define instance methods. For example:

    Method PrintPerson() [ Language = objectscript ]
    {
     Write !, "Name: ", ..Name
    }
    
    Method PrintPerson() [ Language = python ]
    {
        print("\nName:", self.Name)
    }
    

    If myobj is an instance of the class that defines this method, you can invoke this method as follows:

     Do myobj.PrintPerson()
    myobj.PrintPerson()
    
  • You can validate that the property values comply with the rules given in the property definitions.

    • All objects inherit the instance method %NormalizeObject(), which normalizes all the object's property values. Many data types allow different representations of the same value. Normalization converts a value to its canonical, or normalized, form. %NormalizeObject() returns true or false depending on the success of this operation.

    • All objects inherit the instance method %ValidateObject(), which returns true or false depending on whether the property values comply with the property definitions.

    • All persistent objects inherit the instance method %Save(). When you use the %Save() instance method, the system automatically calls %ValidateObject() first.

    In contrast, when you work at the routine level and do not use classes, your code must include logic to check the type and other input requirements.

  • You can define callback methods to add additional custom behavior when objects are created, modified, and so on.

    For example, to create an instance of a class, you invoke the %New() method of that class. If that class defines the %OnNew() method (a callback method), then InterSystems IRIS automatically also calls that method. The following shows a simple example:

    Method %OnNew() As %Status 
    {
        Write "hi there"
        Return $$$OK
    }
    Method %OnNew() As %Status [ Language = python ]
    {
        print("hi there")
        return True
    }
    

    In realistic scenarios, this callback might perform some required initialization. It could also perform logging by writing to a file or perhaps to a global.

FeedbackOpens in a new tab