Object classes

The following ready-made classes are available.

Object

Generic object class. Uses CoercionTrait, ComparableTrait, IteratorAggregateTrait, JsonSerializableTrait, PropertyAccessTrait, StringableTrait and TypeTrait. Implements Comparable and Stringable interfaces.

Funcitonal traits

The following traits provide functionality. Adding more than one of these traits do not cause conflicts.

CoercionTrait

Trait that support coercing non-float content as input.

ComparableTrait

Implements Comparable and Equalable interfaces. Allows comparing class instances based on internal content.

JsonSerializableTrait

Implements JsonSerializable interface. Provides class properties for JSON encode.

PropertyAccessTrait

Enable accessing source data as object properties.

StringableTrait

Implements Stringable interface. Allows string conversion of class to numeric output.

TypeTrait

Base trait for all traits using object as source. Defines source property, options and the initialize method.

Iterator traits

The following traits provide iterators.

IteratorAggregateTrait

Implements IteratorAggregate and Traversable interfaces. Enables traversing methods such as foreach() by aggregating a Generator.

Defining data source

By default, source data is stored in protected property $o_object_source.

If your class is using another property to keep object data, it may define the source property by setting $o_source_ref to the name of that property. The object traits use this definition;

    protected object $o_object_source;
    protected string $o_source_ref = 'o_object_source';

Example using standard object source.

class MyClass
{
    // Use one or more traits
    use ComparableTrait;
    use PropertyAccessTrait;
    use StringableTrait;

    public function __construct(object $data)
    {
        // Set data provided in constructor.
        $this->o_object_source = $data;
    }
}

$my = new MyClass((object)["a" => 1]);

Example using non-standard object source.

class MyClass
{
    // Use one or more traits
    use ComparableTrait;
    use PropertyAccessTrait;
    use StringableTrait;

    // Define the variable shat should hold object source data
    protected object $my_data_source;

    public function __construct(object $data)
    {
        // Tell the traits where to find the source data.
        $this->o_source_ref = 'my_data_source';

        // Set data provided in constructor.
        $this->my_data_source = $data;
    }
}

$my = new MyClass((object)["a" => 1]);