Last active
April 4, 2024 07:42
-
-
Save Kcko/3fd9c106b91d3034c1ab98e5f9671ff8 to your computer and use it in GitHub Desktop.
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 | |
// https://forum.nette.org/cs/22304-zavislost-jednoho-modelu-na-jinych | |
/** | |
* @param Closure $callback | |
*/ | |
private function doInTransaction(Closure $callback) | |
{ | |
$this->isInTransaction = TRUE; | |
$callback(); | |
$this->isInTransaction = FALSE; | |
} | |
// usage | |
// any class | |
private $isInTransaction = FALSE; | |
/** | |
* @param int $id | |
* @param Player $owner | |
*/ | |
public function __construct($id, Player $owner) | |
{ | |
$this->id = $id; | |
$this->owner = $owner; | |
$this->doInTransaction(function () use ($owner) { | |
$owner->receiveItem($this); | |
}); | |
} | |
/** | |
* @param Player $newOwner | |
*/ | |
public function changeOwner(Player $newOwner) | |
{ | |
if ($this->owner !== $newOwner) { | |
$this->doInTransaction(function () use ($newOwner) { | |
$this->owner->giveItem($this); | |
$this->owner = $newOwner; | |
$this->owner->receiveItem($this); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment