This will do what you ask for:
class Test { private $storage; public function __construct(array &$storage) { $this->storage = &$storage; } public function fn() { $this->storage[0] *= 10; } } $storage = [1]; $a = new Test($storage); $b = new Test($storage); $a->fn(); print_r($a);
Alternative 1
Instead of using an array, you can also use ArrayObject , ArrayIterator or SplFixedArray . Since these are objects, they will be passed by reference. They all implement ArrayAccess so that you can access them through square brackets, e.g.
$arrayObject = new ArrayObject; $arrayObject['foo'] = 'bar'; echo $arrayObject['foo'];
Alternative 2
Instead of using a generic type, use a special type. Find out what you are storing in this array. Is this a Config ? A Registry ? A UnitOfWork ? Find out what it really is. Then make it an object and give it an API that reflects responsibilities. Then enter this object and access it through this API.
See this article by Martin Fowler for some tips on how to make type
source share