PHP code example of synthetism / unit-php

1. Go to this page and download the library: Download synthetism/unit-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

synthetism / unit-php example snippets




declare(strict_types=1);

use Unit\Capabilities;
use Unit\Schema;
use Unit\TeachingContract;
use Unit\ToolSchema;
use Unit\Unit;
use Unit\UnitCore;
use Unit\UnitProps;
use Unit\UnitSchema;
use Unit\Validator;

final class EchoUnit extends Unit
{
    protected function __construct(UnitProps $props)
    {
        parent::__construct($props);
    }

    public static function create(): self
    {
        return new self(UnitProps::create([
            'dna' => UnitSchema::create('echo', '1.0.0', 'Echoes text back'),
            'created' => new \DateTimeImmutable(),
        ]));
    }

    protected function build(): UnitCore
    {
        $capabilities = Capabilities::create($this->id(), [
            'say' => [$this, 'say'],
        ]);

        $schema = Schema::create($this->id(), [
            'say' => ToolSchema::create('say', 'Echo text back', [
                'type' => 'object',
                'properties' => ['text' => ['type' => 'string']],
                '

$math = MathUnit::create();   // exports: add, subtract, multiply, divide
$echo->learn($math->teach());

$echo->can('math.add');            // true  (namespaced)
$echo->execute('math.add', 5, 3);  // 8
$echo->schema()->has('math.add');  // true — learned schemas travel too

$evolved = $echo->evolve('loud-echo');

$evolved->dna()->parent?->id;  // 'echo'  (lineage preserved)
$echo->id();                   // 'echo'  (original unchanged)

use Unit\Event;

$echo->on('tool.*', fn (Event $e) => print($e->type()));
$echo->emitEvent(Event::create('tool.success', ['capability' => 'say']));
bash
composer