Lesson 7: Classes and Objects

Intermediate

Phograph is object-oriented. You can define classes with attributes (data) and methods (behavior). Objects are instances of classes.

Defining a class

A class lives inside a section and has:

In JSON format, a class looks like:

{
  "name": "Point",
  "attributes": [
    { "name": "x", "default": 0 },
    { "name": "y", "default": 0 }
  ],
  "methods": [...]
}

Instance generators

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.

new Point Point object (x=0, y=0)

Get and Set nodes

To read or write an object’s attributes, use Get and Set nodes.

[object] get x outputs: (object, x-value) [object] [value] set x outputs: (modified object)

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).

Class methods

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.

[point object] Point/distance class method call (result value)

Walkthrough: Bank Account

Load Data Structures > Bank Account from the Example Browser.

This example defines a BankAccount class with:

The main method:

  1. Creates a new BankAccount.
  2. Sets the owner to "Alice".
  3. Calls deposit(100).
  4. Calls get-balance and returns the result.

Inheritance

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 }
  ]
}

Data-determined dispatch

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.

Exercise 7.1 — Point class

Define a Point class with attributes x and y.

  1. Create a method Point/distance-from-origin (1 input, 1 output) that computes sqrt(x^2 + y^2).
  2. Inside: use get x and get y to extract coordinates.
  3. Compute x*x + y*y using * and +.
  4. If you have a sqrt primitive, use it. Otherwise, just return x*x + y*y for now.
  5. In main: create a Point, set x=3 and y=4, call distance-from-origin.
  6. Expected result: 25 (or 5 if using sqrt).

Exercise 7.2 — Counter class

  1. Define a Counter class with attribute count (default 0).
  2. Add method Counter/increment that adds 1 to count and returns the object.
  3. Add method Counter/get-count that returns the count value.
  4. In main: create a Counter, call increment 3 times (chain the calls), then get-count.
  5. Result should be 3.

Exercise 7.3 — Init method

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.

  1. Add an init method to Point that takes x and y as inputs (after self) and sets both attributes.
  2. In main: create a Point with inputs 3 and 4 (wire constants to the instance generator).
  3. Get and log x. It should be 3.

Key Concepts