Skip to content

Instantly share code, notes, and snippets.

@Kcko
Last active April 4, 2024 07:42
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 Kcko/3fd9c106b91d3034c1ab98e5f9671ff8 to your computer and use it in GitHub Desktop.
Save Kcko/3fd9c106b91d3034c1ab98e5f9671ff8 to your computer and use it in GitHub Desktop.
<?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