Skip to content

Instantly share code, notes, and snippets.

@vertexvaar
Created March 21, 2026 17:43
Show Gist options
  • Select an option

  • Save vertexvaar/5916260f780409000483e53e707c0dc9 to your computer and use it in GitHub Desktop.

Select an option

Save vertexvaar/5916260f780409000483e53e707c0dc9 to your computer and use it in GitHub Desktop.
Asymmetric Property Nightmare
<?php
namespace VerteXVaaR;
trait DelegatedProperty
{
public mixed $value {
set(mixed $newValue) {
if ($newValue instanceof PropertyContainer) {
$this->value = $newValue;
return;
}
$this->value->set($newValue);
}
get => $this->value->get();
}
}
<?php
$thing = new Thing();
$accessor = fn(mixed $value = Undefined::NONE) => $value === Undefined::NONE ? $thing->name : $thing->name = $value;
$input = new Input($accessor);
$input->value = 'New Value';
<?php
namespace VerteXVaaR;
use Closure;
use Psr\Http\Message\ServerRequestInterface;
use VerteXVaaR\BlueValidation\Rule\Rule;
use VerteXVaaR\BlueValidation\ValidationError;
class Input
{
use DelegatedProperty;
public function __construct(mixed &$value)
{
$this->value = new PropertyContainer($value);
}
}
<?php
namespace VerteXVaaR;
use Closure;
class PropertyContainer
{
public function __construct(protected mixed &$initialValue) {}
public function set(mixed $newValue): void
{
if ($this->initialValue instanceof Closure) {
($this->initialValue)($newValue);
return;
}
$this->initialValue = $newValue;
}
public function get(): mixed
{
if ($this->initialValue instanceof Closure) {
return ($this->initialValue)();
}
return $this->initialValue;
}
}
<?php
namespace VerteXVaaR;
class Thing
{
public string $name {
get => $this->name;
set(string $cname) => $this->name = $name;
}
}
<?php
namespace VerteXVaaR;
enum Undefined
{
case NONE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment