opulencephp/Opulence

Ioc container. Remove 'Reference Parameter' support

Closed this issue · 2 comments

// PHP forces a reference operator when passing parameters by reference via an array
if ($parameter->isPassedByReference()) {
$resolvedParameters[] = &$resolvedParameter;
} else {
$resolvedParameters[] = $resolvedParameter;
}

1. it doesn't work

I checked: HEAD, v1.0.0, v1.0.0-rc2

1.1 test is wrong

ContainerTest::testResolvingClassWithReferenceParameter should be something like:

public function testResolvingClassWithReferenceParameter()
{
    // preparing
    $bar = new Bar();
    $this->container->bindInstance($this->fooInterface, $bar);
    /** @var ConstructorWithReference $instance1 */
    $instance1 = $this->container->resolve($this->constructorWithReference);
    /** @var ConstructorWithReference $withRef2 */
    $instance2 = $this->container->resolve($this->constructorWithReference);
    $this->assertInstanceOf($this->constructorWithReference, $instance1);
    $this->assertInstanceOf($this->constructorWithReference, $instance2);
    $this->assertNotSame($instance1, $instance2);
    $this->assertSame($instance1->getFoo(), $instance2->getFoo());

    // start useful check
    $instance1->setFoo(new Bar());
    $this->assertSame($instance1->getFoo(), $instance2->getFoo());
    $instance2->setFoo(new Bar());
    $this->assertSame($instance1->getFoo(), $instance2->getFoo());
}

2. other containers don't support this

  • illuminate/container
  • symfony/dependency-injection
    but I could be wrong

3. this is a really dangerous feature (imho)

So, you're suggesting to replace

// PHP forces a reference operator when passing parameters by reference via an array
if ($parameter->isPassedByReference()) {
$resolvedParameters[] = &$resolvedParameter;
} else {
$resolvedParameters[] = $resolvedParameter;
}
with $resolvedParameters[] = $resolvedParameter;?

So, you're suggesting to replace

// PHP forces a reference operator when passing parameters by reference via an array
if ($parameter->isPassedByReference()) {
$resolvedParameters[] = &$resolvedParameter;
} else {
$resolvedParameters[] = $resolvedParameter;
}
with $resolvedParameters[] = $resolvedParameter;?

yes, and remove ContainerTest::testResolvingClassWithReferenceParameter test