Skip to main content

class

Classes in Soul are defined using the sanctuary keyword and provide object-oriented programming capabilities. They encapsulate data and behavior, support inheritance, and can be extended with traits.

Basic Class Definition

Classes are declared with the sanctuary keyword:

Creating Class Instances

Instances are created using the .new() method:

Constructor Method

The __genesis__ method is the constructor and is called when creating new instances:

Class Inheritance

Classes can inherit from other classes using extends:

Using Super

The super keyword calls methods from the parent class:

Static Methods

Classes can have static methods that belong to the class rather than instances:

Class with Traits

Classes can use traits for code reuse:

Private-like Methods

While Soul doesn’t have true private methods, you can use naming conventions:

Class Properties

Properties can be accessed and modified directly:

Class Methods Calling Other Methods

Methods can call other methods within the same class:

Best Practices

  1. Use meaningful class names: UserManager instead of Manager
  2. Initialize all properties: Set initial values in __genesis__
  3. Use super appropriately: Call parent methods when extending classes
  4. Keep methods focused: Each method should have a single responsibility
Classes provide a powerful way to organize and structure your Soul applications using object-oriented principles.