Supporting object-orientated programming, a function block can be derived from another function block. This means a function block can extend another, thus automatically getting the methods/properties/actions/transitions and variables of the basing function block in addition to its own.
The extension is performed by using the keyword EXTENDS
in the declaration of a function block. You can choose the EXTENDS
option already during adding a function block to the project via the dialog box.
For further information, refer to the sections describing how to add a method, a property, an action, or a transition.
FUNCTION_BLOCK <function block name> EXTENDS <function block name>
This is followed by the declaration of the variables.
Definition of function block fbA
FUNCTION_BLOCK fbA
VAR_INPUT
x:int;
END_VAR
...
Definition of function block fbB
FUNCTION_BLOCK fbB EXTENDS fbA
VAR_INPUT
ivar: INT := 0;
END_VAR
...
EXTENDS
Extension by EXTENDS
means:
fbB
contains all data and methods which are defined by fbA
. An instance of fbB
can now be used in any context where a function block of type fbA
is expected.
fbB
is allowed to override the methods/properties/actions/transitions defined in fbA
. This means: fbB
can declare a method with the same name and the same inputs and output as declared by A
.
fbB
is not allowed to declare function block variables with the same name as used in fbA
. In this case, the compiler will generate an error message.
fbA
variables and methods can be accessed directly within an fbB
scope by using the SUPER pointer (SUPER^.<method>
).
FUNCTION_BLOCK FB_Base
VAR_INPUT
END_VAR
VAR_OUTPUT
iCnt : INT;
END_VAR
VAR
END_VAR
THIS^.METH_DoIt();
THIS^.METH_DoAlso();
METHOD METH_DoIt : BOOL
VAR
END_VAR
iCnt := -1;
METH_DoIt := TRUE;
METHOD METH_DoAlso : BOOL
VAR
END_VAR
METH_DoAlso := TRUE;
FUNCTION_BLOCK FB_1 EXTENDS FB_Base
VAR_INPUT
END_VAR
VAR_OUTPUT
END_VAR
VAR
END_VAR
// Calls the method defined under FB_1
THIS^.METH_DoIt();
THIS^.METH_DoAlso();
// Calls the method defined under FB_Base
SUPER^.METH_DoIt();
SUPER^.METH_DoAlso();
METHOD METH_DoIt : BOOL
VAR
END_VAR
iCnt := 1111;
METH_DoIt := TRUE;
PROGRAM PLC_PRG
VAR
Myfb_1: FB_1;
iFB: INT;
iBase: INT;
END_VAR
Myfb_1();
iBase := Myfb_1.iCnt_Base;
iFB := Myfb_1.iCnt_THIS;