Skip to content

Instantly share code, notes, and snippets.

@dg
Created February 8, 2023 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dg/332cdd51bdf7d66a6d8003b134508a38 to your computer and use it in GitHub Desktop.
Save dg/332cdd51bdf7d66a6d8003b134508a38 to your computer and use it in GitHub Desktop.
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