PHP code example of psrly / huggables

1. Go to this page and download the library: Download psrly/huggables 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/ */

    

psrly / huggables example snippets




srly\Hugger;

// all hugger implementations can be given an optional hug limit and name

// simple silent hugger (no side effects, no state change)
$silent = new Hugger\Silent;

// simple verbose hugger (writes to output on hugging)
$verbose = new Hugger\Verbose;

$verbose->hug($silent);
// -> "X is being hugged by Y"
// -> "X is hugging back Y"

// simple satisfiable hugger (increments satisfaction on hugging)
$satisfactionThreshold = 3; // satisfied after hugging three times
$satisfiable = Satisfiable($satisfactionThreshold);

// hugged 0 times = not satisfied 
$satisfiable->isSatisfied(); // false

$satisfiable->hug($silent);

// hugged 1 time = not satisfied
$satisfiable->isSatisfied(); // false

$satisfiable->hug($silent);
$satisfiable->hug($silent);

// hugged 3 times = satisfied
$satisfiable->isSatisfied(); // true



use Psrly\Hugger;

final class MyHugger extends Hugger
{
    protected function onBeingHuggedBy(Huggable $h): void
    {
        // change state / cause side effects on being hugged
    }

    protected function onHuggingBack(Huggable $h): void
    {
        // change state / cause side effects on hugging back
    }
}