I designed a class that can easily pass references.
<?php
#### Problem 1
function problem(&$value)
{
}
problem(1); // cannot be passed by reference
#### Problem 2
class problem2
{
static function __callStatic($name, &$arguments) // cannot take arguments by reference
{
}
}
?>
My solution ??
<?php
class Reference
{
function __construct(public mixed &$data)
{
}
/**
* create
*
* @param mixed $data
* @return self
*/
static function &create(mixed &$data): self
{
if ($data instanceof self) {
return $data;
}
$r = new self($data);
return $r;
}
/**
* get value
*
* @param mixed $reference
* @return mixed
*/
static function &get(mixed &$reference): mixed
{
if ($reference instanceof self) {
return $reference->data;
}
return $reference;
}
}
#### Problem solving 1 ####
function test($value)
{
$value = &Reference::get($value); // values OR reference
$value = "test-$value";
return $value;
}
echo test(1), PHP_EOL; // test-1
$val = 2;
echo test(Reference::create($val)), PHP_EOL; // test-2
echo $val, PHP_EOL; // test-2
#### Problem solving 2 ####
class TestCall
{
static function __callStatic($name, $arguments)
{
$value = &Reference::get($arguments[0]);
$value = "$name-$value";
return $value;
}
}
echo TestCall::test(3), PHP_EOL; // test-3
$val = 4;
echo TestCall::test(Reference::create($val)), PHP_EOL; // test-4
echo $val, PHP_EOL; // test-4