Created
February 8, 2023 17:22
-
-
Save dg/332cdd51bdf7d66a6d8003b134508a38 to your computer and use it in GitHub Desktop.
Nette Events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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