User-defined classes

A user class is defined in a file of such a format:


# optional
or static   [3.4.1]

# optional

# optional
# user-defined class can't be based on system classes

# recommended way to name
 method-constructor of the class

other methods are defined

Module can be linked (see "Linking modules") to any file, which will then be able to use the class defined here.
If unknown class was specified, the method
autouse of class MAIN will be called and specified class name will be passed to it as the only parameter. [3.4.0]

@CLASS is not specified, the file will define a number of additional operators.

If method

is defined, it will be automatically called as a static method (so-called static constructor) each time the class is loaded. It is used to initialize static fields of the class.

Note: result of the method's work is ignored, i.e. doesn't get anywhere.
Note: method @auto[] is not inherited .

If method is defined to receive the parameter:
In that parameter Parser will pass full name of file containing the method.

Created classes inherit methods of parent classes. Inherited methods can be redefined.

In case one user class must use another one as parent, the file with the parent class should be linked to it, and parent class - declared as base (

To use methods and fields of parent classes, the following constructions should be used:

^class:method[parameters]-to call a method of parent class (note: although the syntax of calling such a method looks like the syntax of calling a static method, in fact, in case of dynamic method, the method of parent class will be called dynamically). To refer to the nearest parent class (base class) you may use constructions ^BASE:constructor[parameters] and ^BASE:method[parameters].
Note: similary base class properties can be accessed -
$BASE:property  $BASE:property[value].   [3.4.5]

Using @OPTIONS you can set additional class behaviour.
Thus option locals automatically declare all variables in all methods of this class as local. If this option specified you must use system variable
self for accessing to class or object's field.

With partial option you can allow future class modifications. If specified and you load new file with use operator which contain the same class name and the same option, the methods from this file will be added to previously loaded class instead of creating a new one with the same name. This option can be useful for loading huge and seldom used methods to the class by demand.

With static and dynamic options you can specify the allowed methods' call types. All methods could be called statically or dynamically by default which could be unsafe.

Note: trailing white-space characters in meta-comands @USE, @CLASS, @BASE, @OPTIONS will be ignored [3.4.1]

Working with variables in static methods
Value of the variable ($name) is searched for in:
·the list of local variables;  
·the current class or its parent classes.  

The value will be assigned (
$name[value]) to already existing variable (see the search area given above), if it does exist. Otherwise, a new variable (field) will be created within the current class.

Working with variables in dynamic methods
Value of the variable ($name) is searched for in:
·the list of local variables;  
·the current object;  
·the current object class or its parent classes.  

The value will be assigned (
$name[value]) to already existing local variable, if it does exist. Otherwise, the value will be assigned to a variable (field) within the current object.   [3.4.5]

Note: try to avoid using fields of class beyond the methods of the class except simplest cases. We should try to communicate with an object through its methods only.

Object's system field: CLASS
$class:CLASS-contains reference to the object's class

You may need this value to specify the scope of code's compilation (cf. "Process. Compiling and processing string").

This reference can also be used to retrieve static fields of the class, for example:



As a result, value of
$cookie:field be output.

Object's class name: CLASS_NAME   [3.2.2]
$object.CLASS_NAME-contains object's class name


This example prints 'string'.

Copyright © 1997–2017 Art. Lebedev Studio | Last updated: 14.04.2017