Skip to content

Instantly share code, notes, and snippets.

@dg
Created February 8, 2023 17:22
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Nette Events
<?php
class Circle
{
public array $onChange = [];
public float $radius = 0;
public function setRadius(float $radius): void
{
foreach ($this->onChange as $handler) {
$handler($this, $radius);
}
// alternatively: Nette\Utils\Arrays::invoke($this->onChange, $this, $radius);
$this->radius = $radius;
}
}
$circle = new Circle;
// add event handler
$circle->onChange[] = function (Circle $circle, float $newValue): void {
echo 'change has occurred';
};
$circle->setRadius(10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment