Intermediate
Phograph is object-oriented. You can define classes with attributes (data) and methods (behavior). Objects are instances of classes.
A class lives inside a section and has:
self).In JSON format, a class looks like:
{
"name": "Point",
"attributes": [
{ "name": "x", "default": 0 },
{ "name": "y", "default": 0 }
],
"methods": [...]
}
An instance generator node creates a new object of a class. Its label is the class name (prefixed with “new” in the canvas). It produces one output: the new object with default attribute values.
To read or write an object’s attributes, use Get and Set nodes.
Get has 1 input (the object) and 2 outputs: the object (pass-through) and the attribute value. The pass-through lets you chain gets.
Set has 2 inputs (the object and the new value) and 1 output (the modified object).
A class method’s first input is always the object (self). When you
call a class method via a method-call node named
ClassName/MethodName, the engine passes the object as the first input.
Load Data Structures > Bank Account from the Example Browser.
This example defines a BankAccount class with:
owner (string), balance (integer, default 0)deposit (adds to balance), get-balance (returns balance)The main method:
BankAccount.owner to "Alice".deposit(100).get-balance and returns the result.A class can extend a parent class. The child inherits all attributes and methods from the parent. It can override methods by defining a method with the same name.
{
"name": "SavingsAccount",
"parent": "BankAccount",
"attributes": [
{ "name": "interest_rate", "default": 0.05 }
]
}
If a method-call node’s name starts with / (e.g., /speak),
Phograph looks at the class of the first input to decide which method to call.
This is dynamic dispatch — the object determines the behavior.
Define a Point class with attributes x and y.
Point/distance-from-origin (1 input, 1 output)
that computes sqrt(x^2 + y^2).get x and get y to extract coordinates.x*x + y*y using * and +.sqrt primitive, use it. Otherwise, just return x*x + y*y for now.distance-from-origin.Counter class with attribute count (default 0).Counter/increment that adds 1 to count and returns the object.Counter/get-count that returns the count value.3.
If a class has an init method, it is called automatically when an
instance is created. The instance generator node can accept inputs which are
passed as extra arguments to init.
init method to Point that takes x and y as inputs (after self)
and sets both attributes.3./methodName calls the method based on the object’s class.